Refactor logging in log_operation to support context-based logging and fallback to standard logger
This commit is contained in:
@@ -18,9 +18,20 @@ logger = logging.getLogger(__name__)
|
||||
def log_operation(level: str, message: str, context=None, **context_vars):
|
||||
"""Centralized logging with context, supporting file and console logging."""
|
||||
context_str = ' '.join(f"{k}={v}" for k, v in context_vars.items() if v is not None)
|
||||
full_message = f"[{time.time()}] {message} {context_str}".strip()
|
||||
full_message = f"{message} {context_str}".strip()
|
||||
|
||||
# Log via logger
|
||||
# Use ctx.logger if context is available (Motia III FlowContext)
|
||||
if context and hasattr(context, 'logger'):
|
||||
if level == 'info':
|
||||
context.logger.info(full_message)
|
||||
elif level == 'warning':
|
||||
context.logger.warning(full_message)
|
||||
elif level == 'error':
|
||||
context.logger.error(full_message)
|
||||
elif level == 'debug':
|
||||
context.logger.debug(full_message)
|
||||
else:
|
||||
# Fallback to standard logger
|
||||
if level == 'info':
|
||||
logger.info(full_message)
|
||||
elif level == 'warning':
|
||||
|
||||
Reference in New Issue
Block a user