qrunch.common.kfy_logging

Module that defines a logger that is used throughout the SDK.

The logger can be used by the user of the SDK or the user can use their own defined standard Python logger if needed. The setup_logger method can be used to create a default streaming logger with a standard format, and optionally also add a handler that saves logs to a file. When scripts are run through the SDK’s main function, the SDK will automatically call the setup and ensure correct logging. This will make sure that remote logs when running a hybrid is send to Cloudwatch and stored in a file that is part of the final result. If scripts are not run through the SDK’s main function the user is responsible for setting up logging that is retrievable by the user.

Functions

setup_logger([level, log_file])

Add a default configuration to the root logger.

suppress_logging_warning(message_substring)

Suppress logger warnings containing a given substring.

Classes

CustomFormatter

Custom logging formatter that uses the SDK's standard logging format.

class CustomFormatter

Bases: Formatter

Custom logging formatter that uses the SDK’s standard logging format.

__init__(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)

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.

converter()
localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,

tm_sec,tm_wday,tm_yday,tm_isdst)

Convert seconds since the Epoch to a time tuple expressing local time. When ‘seconds’ is not passed in, convert the current time instead.

default_msec_format = '%s,%03d'
default_time_format = '%Y-%m-%d %H:%M:%S'
format(record: LogRecord) str

Format a log record.

Parameters:

record (LogRecord) – Log record to format .

Returns:

formatted log record as a string.

Return type:

str

formatException(ei)

Format and return the specified exception information as a string.

This default implementation just uses traceback.print_exception()

formatMessage(record)
formatStack(stack_info)

This method is provided as an extension point for specialized formatting of stack information.

The input data is a string as returned from a call to traceback.print_stack(), but with the last trailing newline removed.

The base implementation just returns the value passed in.

formatTime(record, datefmt=None)

Return the creation time of the specified LogRecord as formatted text.

This method should be called from format() by a formatter which wants to make use of a formatted time. This method can be overridden in formatters to provide for any specific requirement, but the basic behaviour is as follows: if datefmt (a string) is specified, it is used with time.strftime() to format the creation time of the record. Otherwise, an ISO8601-like (or RFC 3339-like) format is used. The resulting string is returned. This function uses a user-configurable function to convert the creation time to a tuple. By default, time.localtime() is used; to change this for a particular formatter instance, set the ‘converter’ attribute to a function with the same signature as time.localtime() or time.gmtime(). To change it for all formatters, for example if you want all logging times to be shown in GMT, set the ‘converter’ attribute in the Formatter class.

usesTime()

Check if the format uses the creation time of the record.

setup_logger(level: int = 25, log_file: Path | None = None) None

Add a default configuration to the root logger.

Adds a default configuration to the root logger, which will be used by all loggers in the code. If a log file path is specified it stores all logs in the specified file as well.

Parameters:
  • level (LogLevel) – The log level to use. Only logs with the given log level or higher are logged.

  • log_file (Optional[Path]) – Path to file to store the logs in.

Return type:

None

suppress_logging_warning(message_substring: str) Iterator[None]

Suppress logger warnings containing a given substring.

A context manager to temporarily suppress logger warnings containing a given substring

Parameters:

message_substring (str) – Substring of the warning message to suppress.

Return type:

Iterator[None]