memoryscope.core.utils.logger
- class memoryscope.core.utils.logger.Logger(name: str, level: int = 20, format_style: str = '%(asctime)s %(levelname)s [%(module)s:%(lineno)d] %(message)s', date_format_style: str = '%Y-%m-%d %H:%M:%S', to_stream: bool = False, to_file: bool = True, file_mode: str = 'w', file_type: str = 'log', dir_path: str = 'log', max_bytes: int = 1073741824, backup_count: int = 10)[源代码]
基类:
Logger
The Logger class handle the stream of information or errors in activities.
- __init__(name: str, level: int = 20, format_style: str = '%(asctime)s %(levelname)s [%(module)s:%(lineno)d] %(message)s', date_format_style: str = '%Y-%m-%d %H:%M:%S', to_stream: bool = False, to_file: bool = True, file_mode: str = 'w', file_type: str = 'log', dir_path: str = 'log', max_bytes: int = 1073741824, backup_count: int = 10)[源代码]
Initializes the Logger instance, setting up handlers for console and file logging based on provided parameters.
- 参数:
name (str) -- Identifier for the logger.
level (int, optional) -- Logging level. Defaults to logging.INFO.
format_style (str, optional) -- Log message format. Defaults to LOG_FORMAT constant.
date_format_style (str, optional) -- Date format for logs. Defaults to DATE_FORMAT constant.
to_stream (bool, optional) -- Enables console logging. Defaults to True.
to_file (bool, optional) -- Enables file logging. Defaults to True.
file_mode (str, optional) -- File open mode. Defaults to 'w'.
file_type (str, optional) -- Log file extension type. Defaults to 'log'.
dir_path (str, optional) -- Directory for log files. Defaults to 'log'.
max_bytes (int, optional) -- Maximum log file size before rotation. Defaults to 1GB.
backup_count (int, optional) -- Number of rotated log files to retain. Defaults to 10.
- close()[源代码]
Closes all handlers associated with this logger instance.
This method iterates over the handlers attached to the logger and calls their close method to ensure that any system resources used by the handlers are freed properly.
- set_trace_id(trace_id: str)[源代码]
Sets the trace ID for the logger. If the provided trace ID is longer than 8 characters, it will be truncated to the first 8 characters.
- 参数:
trace_id (str) -- The trace identifier to be associated with the logs.
- makeRecord(name, level, fn, lno, msg, args, exc_info, func=None, extra=None, sinfo=None)[源代码]
Creates a log record with additional trace_id included in the extra information.
This method extends the default behavior of creating a log record by adding a trace_id from the logger instance to the record's extra data, allowing for traceability within logged data.
- 参数:
name (str) -- The name of the logger.
level (int) -- The logging level of the record.
fn (str) -- The name of the function containing the logging call.
lno (int) -- The line number at which the logging call was made.
msg (str) -- The logged message, before formatting.
args (tuple) -- The arguments to the log message.
exc_info (tuple) -- Exception information or None.
func (function) -- The function where the logging call was made. Defaults to None.
extra (dict) -- Additional information for the log record. Defaults to None.
sinfo (str) -- Stack trace information or None.
- 返回:
The created log record with potentially enriched 'extra' field.
- 返回类型:
logging.LogRecord
- classmethod get_logger(name: str | None = None, **kwargs)[源代码]
Retrieves or creates a logger instance with the specified name and configurations.
If no name is provided, it defaults to the first registered logger's name or 'default' if none exist. This method ensures that only one logger instance exists per name by reusing existing instances stored in LOGGER_DICT.
- 参数:
name (str, optional) -- The name of the logger. Defaults to None, which triggers auto-naming logic.
**kwargs -- Additional keyword arguments to configure the logger.
- 返回:
The requested or newly created logger instance.
- 返回类型: