LogFormatter
File: base/log/log_formatter.py
This file implements a basic log formatter that formats log messages defaulting to a specified pattern used by the GLog library (C++).
Classes:
-
LogFormatter
–A class for formatting log messages with customizable patterns.
LogFormatter
A class for formatting log messages with customizable patterns.
This formatter allows developers to define a custom log message prefix format, similar to the GLog library. The format can include placeholders for date, time, severity, file name, and line number.
Attributes:
-
format_pattern
(str
) –The pattern for formatting log messages. If None, a default pattern is used that mimics GLog.
Methods:
-
__init__
–Initializes the LogFormatter with a given format pattern.
-
format
–Formats a log message with the specified severity and message.
Source code in kern_comm_lib/base/log/log_formatter.py
__init__
Initializes the LogFormatter with a given format pattern.
Parameters:
-
format_pattern
(default
, default:None
) –None): The pattern for formatting log messages.
Notes
Default pattern mimics GLog: - %severity% -> First letter of severity (e.g., I, W, E, F) - %Y%m%d -> Full date (year, month, day) - %H:%M:%S.%f -> Time with microseconds - [%F:%L] -> File name and line number of the log call
Source code in kern_comm_lib/base/log/log_formatter.py
format
Formats a log message with the specified severity and message.
This method generates a log message prefix based on the format pattern and replaces placeholders with actual values such as the current date, time, severity, file name, and line number.
Parameters:
-
severity
(LogSeverity
) –The severity level of the log message.
-
message
(str
) –The log message to be formatted.
Returns:
-
str
–The formatted log message with the prefix.
Notes
- The file name and line number are determined using the
inspect
module. - If the stack depth is insufficient, "unknown" and -1 are used as defaults for the file name and line number, respectively.