duck.logging.logger

Logging module for Duck with console color support using colorama module.

Module Contents

Classes

Logger

Logging class.

Functions

expand_exception

Expands an exception to show the traceback and more information.

handle_exception

Decorator that executes a function or callable. If an exception occurs, logs the exception to console and file or both.

log

Pretty log a message to console.

Args:
    msg (str): The message to log.
    prefix (str): The prefix to prepend to the message.
    level (int): The log level of the message.
    use_colors (bool): Whether to log message with some colors, ie, red for Errors, Yellow for warnings, etc
    custom_color (string): The custom color to use. Argument `use_colors` is required to use custom color.
    end (str): The log suffix, defaults to `"

log_exception

Logs exception to console and file or both.

log_raw

Logs a message to console as it is without any modifications.

Args:
    msg (str): The message to log.
    level (int): The log level of the message.
    use_colors (bool): Whether to log message with some colors, i.e. red for Errors, Yellow for warnings, etc.
    custom_color (string): The custom color to use.
    The use colors argument is required to use custom color.
    end (str): The log suffix, defaults to `"

should_filter_warning

Simulate Python’s filtering logic for a warning. Returns True if the warning would be filtered (ignored), False otherwise.

warn

This logs a warning to the console. You can filter warnings by using warnings.filterwarnings.

Data

CRITICAL

DEBUG

ERROR

INFO

LOGGING_DIR

LOG_FILE_FORMAT

LOG_TO_FILE

SILENT

SUCCESS

VERBOSE_LOGGING

WARNING

API

duck.logging.logger.CRITICAL

4

duck.logging.logger.DEBUG

1

duck.logging.logger.ERROR

5

duck.logging.logger.INFO

0

duck.logging.logger.LOGGING_DIR

None

duck.logging.logger.LOG_FILE_FORMAT

None

duck.logging.logger.LOG_TO_FILE

None

class duck.logging.logger.Logger

Logging class.

__current_logfile_fd

None

The file descriptor for the current logfile.

classmethod close_logfile()

Closes the current logfile if opened.

classmethod get_current_logfile(raise_if_logging_dir_not_found: bool = True) str

Returns the current log file.

Parameters:

raise_if_logging_dir_not_found – Whether to raise an exception if logging directory is not found.

Returns:

The path to the current log file.

Return type:

str

Raises:

FileNotFoundError – If the log directory does not exist.

classmethod get_current_logfile_fd()

Get the opened file descriptor for the current log file in bytes append mode.

classmethod get_latest_logfile() Optional[str]

Returns the latest created file in LOGGING_DIR.

classmethod log_to_file(data: Union[str, bytes], end: Union[str, bytes] = '\n') str | bytes

This writes data to the log file.

Parameters:
  • data – Data to write.

  • end – The suffix to add to data before writting to file.

Returns:

Data that was written (in bytes).

Return type:

bytes

Raises:

DisallowedAction – If SILENT=False or LOG_TO_FILE=False in settings.

print_lock

‘Lock(…)’

Lock used by log & log_raw functions.

classmethod redirect_console_output()

Redirects all console output (stdout and stderr) to a log file, i.e current log file.

This function locks sys.stdout and sys.stderr so that they cannot be modified by another process.

classmethod undo_console_output_redirect()

Undo redirecting of console output.

duck.logging.logger.SILENT

None

duck.logging.logger.SUCCESS

2

duck.logging.logger.VERBOSE_LOGGING

None

duck.logging.logger.WARNING

3

duck.logging.logger.expand_exception(e: Exception) str

Expands an exception to show the traceback and more information.

Parameters:

e – The exception to expand.

Returns:

The expanded exception.

Return type:

str

duck.logging.logger.handle_exception(func: Callable)

Decorator that executes a function or callable. If an exception occurs, logs the exception to console and file or both.

Parameters:

func – The function to be decorated.

Returns:

The wrapped function with exception handling.

Return type:

callable

duck.logging.logger.log(msg: str, prefix: str = '[ * ]', level: int = INFO, use_colors: bool = True, custom_color: str = None, end: str = '\n')
Pretty log a message to console.

Args:
    msg (str): The message to log.
    prefix (str): The prefix to prepend to the message.
    level (int): The log level of the message.
    use_colors (bool): Whether to log message with some colors, ie, red for Errors, Yellow for warnings, etc
    custom_color (string): The custom color to use. Argument `use_colors` is required to use custom color.
    end (str): The log suffix, defaults to `"

“` for newline.

duck.logging.logger.log_exception(e: Exception)

Logs exception to console and file or both.

duck.logging.logger.log_raw(msg: str, level: int = INFO, use_colors: bool = True, custom_color: str = None, end: str = '\n')
Logs a message to console as it is without any modifications.

Args:
    msg (str): The message to log.
    level (int): The log level of the message.
    use_colors (bool): Whether to log message with some colors, i.e. red for Errors, Yellow for warnings, etc.
    custom_color (string): The custom color to use.
    The use colors argument is required to use custom color.
    end (str): The log suffix, defaults to `"

“` for newline.

duck.logging.logger.should_filter_warning(category, message, module=None, lineno=0)

Simulate Python’s filtering logic for a warning. Returns True if the warning would be filtered (ignored), False otherwise.

duck.logging.logger.warn(message: str, category: Warning = UserWarning, use_colors: bool = True, module=None, lineno=0)

This logs a warning to the console. You can filter warnings by using warnings.filterwarnings.