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

blue(string)

Adds ANSI escape codes to print a string in blue around the string.

bold(string)

Adds ANSI escape codes to print a string in bold around the string.

configure_console_logger([level, name, ...])

Configures a simple console logger with the givel level.

configure_file_logger([level])

Configures a simple console logger with the givel level.

critical(message, *args, **kwargs)

Prints a message with log level critical.

cyan(string)

Adds ANSI escape codes to print a string in cyan around the string.

darkgreen(string)

Adds ANSI escape codes to print a string in blue around the string.

debug(message, *args, **kwargs)

Prints a message with log level debug.

deprecated(message, *args, **kwargs)

Prints a message with custom log level DEPRECATED.

enable_file_logging()

Enables file logging

error(message, *args, **kwargs)

Prints a message with log level error.

get_format_fn(name_format)

Returns the format function for the given format name.

green(string)

Adds ANSI escape codes to print a string in green around the string.

info(message, *args, **kwargs)

Prints a message with log level info.

magenta(string)

Adds ANSI escape codes to print a string in magenta around the string.

number_to_str(number[, decimals])

Formats a number to a string with the given number of decimals.

orange(string)

Adds ANSI escape codes to print a string in orange around the string.

purple(string)

Adds ANSI escape codes to print a string in purple around the string.

red(string)

Adds ANSI escape codes to print a string in red around the string.

remove_color_escape_codes(text)

Removes ANSI color escape codes from the given string.

set_file_logger_directory(directory)

Sets the log level of the logger.

set_level(level)

Context manager to temporarily set the log level for the logger.

success(message)

Prints a message to the console in green.

warning(message, *args, **kwargs)

Prints a message with log level warning.

white(string)

Adds ANSI escape codes to print a string in white around the string.

yellow(string)

Adds ANSI escape codes to print a string in yellow around the string.

Classes

CustomFormatter([name, color, name_color])

Custom formatter to use different format strings for different log levels

class zea.log.CustomFormatter(name=None, color=True, name_color='darkgreen')[source]

Bases: Formatter

Custom 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 or string.Template formatting in your format string.

Changed in version 3.2: Added the style parameter.

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.blue(string)[source]

Adds ANSI escape codes to print a string in blue around the string.

zea.log.bold(string)[source]

Adds ANSI escape codes to print a string in bold around the string.

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.critical(message, *args, **kwargs)[source]

Prints a message with log level critical.

zea.log.cyan(string)[source]

Adds ANSI escape codes to print a string in cyan around the string.

zea.log.darkgreen(string)[source]

Adds ANSI escape codes to print a string in blue around the string.

zea.log.debug(message, *args, **kwargs)[source]

Prints a message with log level debug.

zea.log.deprecated(message, *args, **kwargs)[source]

Prints a message with custom log level DEPRECATED.

zea.log.enable_file_logging()[source]

Enables file logging

zea.log.error(message, *args, **kwargs)[source]

Prints a message with log level error.

zea.log.get_format_fn(name_format)[source]

Returns the format function for the given format name.

zea.log.green(string)[source]

Adds ANSI escape codes to print a string in green around the string.

zea.log.info(message, *args, **kwargs)[source]

Prints a message with log level info.

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.red(string)[source]

Adds ANSI escape codes to print a string in red around the string.

zea.log.remove_color_escape_codes(text)[source]

Removes ANSI color escape codes from the given string.

zea.log.set_file_logger_directory(directory)[source]

Sets the log level of the logger.

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")
zea.log.success(message)[source]

Prints a message to the console in green.

zea.log.warning(message, *args, **kwargs)[source]

Prints a message with log level warning.

zea.log.white(string)[source]

Adds ANSI escape codes to print a string in white around the string.

zea.log.yellow(string)[source]

Adds ANSI escape codes to print a string in yellow around the string.