Skip to content

Commit f9d8bbd

Browse files
committed
Enhance logging in authentication process to reduce verbosity for commonly polled API endpoints. Implement conditional logging to limit debug messages for polling endpoints, improving performance and reducing log clutter. Maintain detailed logging for other request paths to ensure comprehensive monitoring.
1 parent 6862a11 commit f9d8bbd

File tree

1 file changed

+46
-17
lines changed

1 file changed

+46
-17
lines changed

src/primary/auth.py

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -291,26 +291,36 @@ def authenticate_request():
291291
health_check_path = "/api/health"
292292
ping_path = "/ping"
293293

294-
logger.debug(f"authenticate_request: checking path '{request.path}'")
294+
# Check if this is a commonly polled API endpoint to reduce log verbosity
295+
is_polling_endpoint = any(endpoint in request.path for endpoint in [
296+
'/api/logs/', '/api/cycle/', '/api/hourly-caps', '/api/swaparr/status'
297+
])
298+
299+
if not is_polling_endpoint:
300+
logger.debug(f"authenticate_request: checking path '{request.path}'")
295301

296302
# FIRST: Always allow setup and user page access - this handles returns from external auth like Plex
297303
if request.path in ['/setup', '/user']:
298-
logger.debug(f"Allowing setup/user page access for path: {request.path}")
304+
if not is_polling_endpoint:
305+
logger.debug(f"Allowing setup/user page access for path: {request.path}")
299306
return None
300307

301308
# Skip authentication for static files, API setup, health check path, and ping
302309
if request.path.startswith((static_path, api_setup_path)) or request.path in (favicon_path, health_check_path, ping_path):
303-
logger.debug(f"Skipping authentication for path '{request.path}' (static/api-setup/health/ping)")
310+
if not is_polling_endpoint:
311+
logger.debug(f"Skipping authentication for path '{request.path}' (static/api-setup/health/ping)")
304312
return None
305313

306314
# If no user exists, redirect to setup
307315
if not user_exists():
308-
logger.debug(f"No user exists, redirecting to setup")
316+
if not is_polling_endpoint:
317+
logger.debug(f"No user exists, redirecting to setup")
309318
return redirect(url_for("common.setup"))
310319

311320
# Skip authentication for login pages and Plex auth endpoints
312321
if request.path.startswith((login_path, api_login_path, api_auth_plex_path)):
313-
logger.debug(f"Skipping authentication for login/plex path '{request.path}'")
322+
if not is_polling_endpoint:
323+
logger.debug(f"Skipping authentication for login/plex path '{request.path}'")
314324
return None
315325

316326
# Load general settings
@@ -329,11 +339,12 @@ def authenticate_request():
329339
general_settings = settings
330340
local_access_bypass = general_settings.get("local_access_bypass", False)
331341
proxy_auth_bypass = general_settings.get("proxy_auth_bypass", False)
332-
logger.debug(f"Local access bypass setting: {local_access_bypass}")
333-
logger.debug(f"Proxy auth bypass setting: {proxy_auth_bypass}")
334342

335-
# Debug print all general settings
336-
logger.debug(f"All general settings: {general_settings}")
343+
# Log settings only for non-polling endpoints to reduce spam
344+
if not is_polling_endpoint:
345+
logger.debug(f"Local access bypass setting: {local_access_bypass}")
346+
logger.debug(f"Proxy auth bypass setting: {proxy_auth_bypass}")
347+
logger.debug(f"All general settings: {general_settings}")
337348
except Exception as e:
338349
logger.error(f"Error loading authentication bypass settings: {e}", exc_info=True)
339350

@@ -343,7 +354,8 @@ def authenticate_request():
343354
return None
344355

345356
remote_addr = request.remote_addr
346-
logger.debug(f"Request IP address: {remote_addr}")
357+
if not is_polling_endpoint:
358+
logger.debug(f"Request IP address: {remote_addr}")
347359

348360
if local_access_bypass:
349361
# Common local network IP ranges
@@ -395,28 +407,45 @@ def authenticate_request():
395407
break
396408

397409
if is_local:
398-
logger.debug(f"Local network access from {remote_addr} - Authentication bypassed! (Local Bypass Mode)")
410+
if not is_polling_endpoint:
411+
logger.debug(f"Local network access from {remote_addr} - Authentication bypassed! (Local Bypass Mode)")
399412
return None
400413
else:
401-
logger.warning(f"Access from {remote_addr} is not recognized as local network - Authentication required")
414+
if not is_polling_endpoint:
415+
logger.warning(f"Access from {remote_addr} is not recognized as local network - Authentication required")
402416
else:
403-
logger.debug("Local Bypass Mode is DISABLED - Authentication required")
417+
if not is_polling_endpoint:
418+
logger.debug("Local Bypass Mode is DISABLED - Authentication required")
404419

405420
# Check for valid session
406421
session_id = session.get(SESSION_COOKIE_NAME)
407422
if session_id and verify_session(session_id):
408-
logger.debug(f"Valid session found for path '{request.path}'")
423+
if not is_polling_endpoint:
424+
logger.debug(f"Valid session found for path '{request.path}'")
409425
return None
410426

411-
logger.debug(f"No valid session for path '{request.path}', session_id: {session_id}")
427+
# Use less verbose logging for polling endpoints
428+
if is_polling_endpoint:
429+
# Only log occasionally for polling endpoints to reduce spam
430+
import random
431+
if random.random() < 0.1: # Log only 10% of polling auth failures
432+
logger.debug(f"No valid session for polling endpoint '{request.path}', session_id: {session_id}")
433+
else:
434+
logger.debug(f"No valid session for path '{request.path}', session_id: {session_id}")
412435

413436
# For API calls, return 401 Unauthorized
414437
if request.path.startswith("/api/"):
415-
logger.debug(f"Returning 401 for API path '{request.path}'")
438+
# Return 401 with less verbose logging for polling endpoints
439+
if is_polling_endpoint:
440+
# Don't log every 401 for polling endpoints
441+
pass
442+
else:
443+
logger.debug(f"Returning 401 for API path '{request.path}'")
416444
return {"error": "Unauthorized"}, 401
417445

418446
# No valid session, redirect to login
419-
logger.debug(f"Redirecting to login for path '{request.path}'")
447+
if not is_polling_endpoint:
448+
logger.debug(f"Redirecting to login for path '{request.path}'")
420449
return redirect(url_for("common.login_route"))
421450

422451
def logout(session_id: str):

0 commit comments

Comments
 (0)