import os from app.core.projscale_logger import logger from app.core.log_context import ctx_session_id, ctx_user_id, ctx_method, ctx_path, ctx_remote, ctx_rid import logging LOG_LEVELS = { 'DEBUG': logging.DEBUG, 'INFO': logging.INFO, 'WARNING': logging.WARNING, 'ERROR': logging.ERROR } IGNORED_ISSUERS = os.getenv('IGNORED_ISSUERS', '').split(',') def make_log(issuer, message, *args, level='INFO', **kwargs): if issuer in IGNORED_ISSUERS: return assert level.upper() in LOG_LEVELS.keys(), f"Unknown log level" _log = getattr(logger, level.lower()) # Merge context variables if not explicitly provided context_fields = { 'sid': kwargs.get('sid') or kwargs.get('session_id') or ctx_session_id.get(), 'user_id': kwargs.get('user_id') or ctx_user_id.get(), 'method': kwargs.get('method') or ctx_method.get(), 'path': kwargs.get('path') or ctx_path.get(), 'remote': kwargs.get('remote') or ctx_remote.get(), 'rid': kwargs.get('rid') or ctx_rid.get(), } # Only include non-empty context for k, v in list(context_fields.items()): if v is None: context_fields.pop(k) # Do not override provided kwargs; merge missing only for k, v in context_fields.items(): kwargs.setdefault(k, v) log_buffer = f"[{issuer if not (issuer is None) else 'System'}] {message}" if args: log_buffer += f" | {args}" if kwargs: log_buffer += f" | {kwargs}" _log(log_buffer)