duck.logging.console

Console Logging Module - Log messages to console only!

This module provides utility functions to log messages to the console in a customizable format. It supports raw logging (plain messages) and formatted logging with prefixes, log levels, and optional colored output.

Features:

  • Log messages with predefined log levels: DEBUG, INFO, WARNING, ERROR, CRITICAL.

  • Support for colored output using ANSI escape codes.

  • Customizable prefixes and colors for better message clarity.

  • Automatically directs error-level logs to stderr and others to stdout.

Dependencies:

  • Requires the colorama library for colored console output.

Constants:

  • CRITICAL (int): Log level for critical messages.

  • ERROR (int): Log level for error messages.

  • WARNING (int): Log level for warning messages.

  • INFO (int): Log level for informational messages.

  • DEBUG (int): Log level for debug messages.

  • SUCCESS (int): Log level for success message.

Functions:

  • log_raw(msg, level, use_colors, custom_color): Logs a plain message.

  • log(msg, prefix, level, use_colors, custom_color): Logs a formatted message with a prefix.

Example Usage:

log("This is an info message.", level=INFO)
log("This is a warning!", level=WARNING)
log("This is an error!", level=ERROR)
log_raw("Raw debug message", level=DEBUG, use_colors=True, custom_color=Fore.MAGENTA)

Module Contents

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

Logs a formatted message to the console with a prefix and optional colors.

Args:
    msg (str): The message to log.
    prefix (str): A prefix to prepend to the message, e.g., '[ * ]', 'INFO', 'ERROR'.
    level (int): The log level of the message (e.g., DEBUG, INFO, WARNING, ERROR, CRITICAL).
    use_colors (bool): Whether to apply color formatting to the message.
    custom_color (str): A custom ANSI color code for the message.
        Requires `use_colors` to be `True`.
    end (str): The log suffix, defaults to `"

log_exception

Logs exception to console and file or both.

log_raw

Logs a raw message to the console without any modifications or prefixes.

Args:
    msg (str): The message to log.
    level (int): The log level of the message (e.g., DEBUG, INFO, WARNING, ERROR, CRITICAL).
    use_colors (bool): Whether to apply color formatting to the message.
    custom_color (str): A custom ANSI color code for the message.
        Requires `use_colors` to be `True`.
    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

RESPECT_SILENT_CONSOLE_LOGS

SILENT

SUCCESS

WARNING

print_lock

API

duck.logging.console.CRITICAL

4

duck.logging.console.DEBUG

1

duck.logging.console.ERROR

5

duck.logging.console.INFO

0

duck.logging.console.RESPECT_SILENT_CONSOLE_LOGS

False

duck.logging.console.SILENT

False

duck.logging.console.SUCCESS

2

duck.logging.console.WARNING

3

duck.logging.console.expand_exception(e: Exception) str[source]

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.console.handle_exception(func: Callable)[source]

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.console.log(msg: str, prefix: str = '[ * ]', level: int = INFO, use_colors: bool = True, custom_color: str = None, end: str = '\n')[source]
Logs a formatted message to the console with a prefix and optional colors.

Args:
    msg (str): The message to log.
    prefix (str): A prefix to prepend to the message, e.g., '[ * ]', 'INFO', 'ERROR'.
    level (int): The log level of the message (e.g., DEBUG, INFO, WARNING, ERROR, CRITICAL).
    use_colors (bool): Whether to apply color formatting to the message.
    custom_color (str): A custom ANSI color code for the message.
        Requires `use_colors` to be `True`.
    end (str): The log suffix, defaults to `"

“` for newline.

duck.logging.console.log_exception(e: Exception)[source]

Logs exception to console and file or both.

duck.logging.console.log_raw(msg: str, level: int = INFO, use_colors: bool = True, custom_color: str = None, end: str = '\n')[source]
Logs a raw message to the console without any modifications or prefixes.

Args:
    msg (str): The message to log.
    level (int): The log level of the message (e.g., DEBUG, INFO, WARNING, ERROR, CRITICAL).
    use_colors (bool): Whether to apply color formatting to the message.
    custom_color (str): A custom ANSI color code for the message.
        Requires `use_colors` to be `True`.
    end (str): The log suffix, defaults to `"

“` for newline.

duck.logging.console.print_lock

‘Lock(…)’

duck.logging.console.should_filter_warning(category, message, module=None, lineno=0)[source]

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

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

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