zea.log¶
Custom zea python logging module.
Wrapper around python logging module to provide a simple interface for logging both to the console and to a file with color support.
Example usage¶
Functions
|
Adds ANSI escape codes to print a string in blue around the string. |
|
Adds ANSI escape codes to print a string in bold around the string. |
|
Configures a simple console logger with the givel level. |
|
Configures a simple console logger with the givel level. |
|
Prints a message with log level critical. |
|
Adds ANSI escape codes to print a string in cyan around the string. |
|
Adds ANSI escape codes to print a string in blue around the string. |
|
Prints a message with log level debug. |
|
Prints a message with custom log level DEPRECATED. |
Enables file logging |
|
|
Prints a message with log level error. |
|
Returns the format function for the given format name. |
|
Adds ANSI escape codes to print a string in green around the string. |
|
Prints a message with log level info. |
|
Adds ANSI escape codes to print a string in magenta around the string. |
|
Formats a number to a string with the given number of decimals. |
|
Adds ANSI escape codes to print a string in orange around the string. |
|
Adds ANSI escape codes to print a string in purple around the string. |
|
Adds ANSI escape codes to print a string in red around the string. |
Removes ANSI color escape codes from the given string. |
|
|
Sets the log level of the logger. |
|
Context manager to temporarily set the log level for the logger. |
|
Prints a message to the console in green. |
|
Prints a message with log level warning. |
|
Adds ANSI escape codes to print a string in white around the string. |
|
Adds ANSI escape codes to print a string in yellow around the string. |
Classes
|
Custom formatter to use different format strings for different log levels |
- class zea.log.CustomFormatter(name=None, color=True, name_color='darkgreen')[source]¶
Bases:
FormatterCustom formatter to use different format strings for different log levels
Initialize the formatter with specified format strings.
Initialize the formatter either with the specified format string, or a default as described above. Allow for specialized date formatting with the optional datefmt argument. If datefmt is omitted, you get an ISO8601-like (or RFC 3339-like) format.
Use a style parameter of ‘%’, ‘{’ or ‘$’ to specify that you want to use one of %-formatting,
str.format()({}) formatting orstring.Templateformatting in your format string.Changed in version 3.2: Added the
styleparameter.- format(record)[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.
- zea.log.configure_console_logger(level='INFO', name=None, color=True, name_color='darkgreen')[source]¶
Configures a simple console logger with the givel level. A usecase is to change the formatting of the default handler of the root logger
- zea.log.configure_file_logger(level='INFO')[source]¶
Configures a simple console logger with the givel level. A usecase is to change the formatting of the default handler of the root logger
- zea.log.darkgreen(string)[source]¶
Adds ANSI escape codes to print a string in blue around the string.
- zea.log.deprecated(message, *args, **kwargs)[source]¶
Prints a message with custom log level DEPRECATED.
- zea.log.magenta(string)[source]¶
Adds ANSI escape codes to print a string in magenta around the string.
- zea.log.number_to_str(number, decimals=2)[source]¶
Formats a number to a string with the given number of decimals.
- zea.log.orange(string)[source]¶
Adds ANSI escape codes to print a string in orange around the string.
- zea.log.purple(string)[source]¶
Adds ANSI escape codes to print a string in purple around the string.
- zea.log.remove_color_escape_codes(text)[source]¶
Removes ANSI color escape codes from the given string.
- zea.log.set_level(level)[source]¶
Context manager to temporarily set the log level for the logger.
Also sets the log level for the file logger if it exists.
- Parameters:
level (str or int) – The log level to set temporarily (e.g., “DEBUG”, “INFO”, logging.WARNING).
- Yields:
None
Example
>>> from zea import log >>> with log.set_level("ERROR"): ... _ = log.info("Info messages will not be shown") ... _ = log.error("Error messages will be shown")