magic_utils.magicLogger package
Submodules
magic_utils.magicLogger.loggingFormatter module
- class magic_utils.magicLogger.loggingFormatter.ColoredFormatter(extra_args: list[str] = None)[source]
Bases:
Formatter- format(record) str[source]
Format the specified record as text.
The record’s attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.
- class magic_utils.magicLogger.loggingFormatter.JsonFormatter(extra_args: list[str] = None)[source]
Bases:
Formatter- format(record) str[source]
Format the specified record as text.
The record’s attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.
magic_utils.magicLogger.loggingManager module
- magic_utils.magicLogger.loggingManager.setup_logger(logger_name: str, log_file_path: str, stream_level: int = 10, log_level: int = 10, stream_in_color: bool = True, stream_formatter: Formatter | Literal['default'] | None = 'default', file_formatter: Formatter | Literal['default'] | None = 'default', log_in_json: bool = True, extra_log_args: list[str] = None, remove_previous_handlers: bool = True, timed_rotating_file_handler_kwargs: dict | None = None) Logger[source]
Configure and initialize a logger with stream and timed rotating file handlers.
- Note:
logger.propagate = False
removes previous handlers from logger
Default format (this isn’t the colored format): [%(asctime)s | %(levelname)s] [%(filename)s | lineno%(lineno)d | %(funcName)s] => %(message)s
- Parameters:
logger_name – The name of the logger to configure.
log_file_path – The filename where logs will be written, with daily rotation. (e.g., ‘/logs/app.jsonl’)
stream_level – The log level for the stream handler (e.g., logging.DEBUG, logging.INFO) Defaults to DEBUG.
log_level – The log level for the file handler (e.g., logging.DEBUG, logging.INFO) Defaults to DEBUG.
stream_in_color – If True, logs to stdout will use colored formatting. Defaults to True.
stream_formatter – The formatter to use for the stream handler. Defaults to ColoredFormatter if stream_in_color is True,
otherwise uses the default formatter when ‘default’ is used. To disable the stream Handler set it to None. :param file_formatter: The formatter to use for the file handler. Defaults to JsonFormatter if log_in_json is True, otherwise uses the default formatter when ‘default’ is used. To disable the stream Handler set it to None. :param log_in_json: If True, logs to file will be written in JSON format. Defaults to True. :param remove_previous_handlers: If True, removes previous handlers from the logger. Defaults to True. :param extra_log_args: List of extra attribute keys to include in the logs (e.g., [‘arg1’, ‘arg2’]).
These keys will be added to the colored formatter output if present.
- Parameters:
timed_rotating_file_handler_kwargs – Kwargs to pass to the TimedRotatingFileHandler constructor. Overwrites all the default kwargs {‘filename’: log_file_name, ‘when’: ‘midnight’, ‘interval’: 1, ‘backupCount’: 3}
- Returns:
None