Skip to content

logging

plantimager.commons.logging Link

Logging System for Application Monitoring

A comprehensive logging system that provides structured, configurable logging capabilities for tracking application events, errors, and performance metrics across different components.

create_logger Link

create_logger(name, level=DEBUG)

Create a logger with the given name and specified logging level.

Parameters:

Name Type Description Default
name str

Name of the logger. This identifies the logger in the hierarchy.

required
level int

Logging level of the logger. Must be one of DEBUG, INFO, WARNING, ERROR, CRITICAL from the logging module. Default is logging.DEBUG.

DEBUG

Returns:

Type Description
Logger

Configured logger object with stream handler and level-specific formatting.

Notes
  • The logger uses colorlog.LevelFormatter for colored output in console
  • Different log levels have different format patterns:
  • DEBUG: Includes timestamp, name, thread, level, message, filename, and line number
  • INFO: Simplified format with timestamp, name, thread, level, and message
  • WARNING/ERROR/CRITICAL: Similar to DEBUG format with file location information

Examples:

>>> from plantimager.commons.logging import create_logger
>>> logger = create_logger("my_app")
>>> logger.info("Application started")
[timestamp] my_app MainThread - INFO: Application started
>>> logger.error("An error occurred")
[timestamp] my_app MainThread - ERROR: An error occurred (logging.py:123)
Source code in plantimager/commons/logging.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def create_logger(name: str, level=logging.DEBUG) -> logging.Logger:
    """Create a logger with the given name and specified logging level.

    Parameters
    ----------
    name : str
        Name of the logger. This identifies the logger in the hierarchy.
    level : int, optional
        Logging level of the logger. Must be one of DEBUG, INFO, WARNING, ERROR, CRITICAL
        from the logging module. Default is logging.DEBUG.

    Returns
    -------
    logging.Logger
        Configured logger object with stream handler and level-specific formatting.

    Notes
    -----
    - The logger uses colorlog.LevelFormatter for colored output in console
    - Different log levels have different format patterns:
      - DEBUG: Includes timestamp, name, thread, level, message, filename, and line number
      - INFO: Simplified format with timestamp, name, thread, level, and message
      - WARNING/ERROR/CRITICAL: Similar to DEBUG format with file location information

    Examples
    --------
    >>> from plantimager.commons.logging import create_logger
    >>> logger = create_logger("my_app")
    >>> logger.info("Application started")
    [timestamp] my_app MainThread - INFO: Application started
    >>> logger.error("An error occurred")
    [timestamp] my_app MainThread - ERROR: An error occurred (logging.py:123)
    """
    # Create a new logger instance with the specified name
    logger = logging.Logger(name)
    # Set the minimum logging level
    logger.setLevel(level)

    # Create a LevelFormatter that uses different format strings based on log level
    # The log_color prefix enables colored output for each level
    formatter = colorlog.LevelFormatter(fmt={
        logging.getLevelName(logging.DEBUG):    "{log_color}[{asctime}] {name} {threadName} - {levelname}: {message} ({filename}:{lineno})",
        logging.getLevelName(logging.INFO):     "{log_color}[{asctime}] {name} {threadName} - {levelname}: {message}",
        logging.getLevelName(logging.WARNING):  "{log_color}[{asctime}] {name} {threadName} - {levelname}: {message} ({filename}:{lineno})",
        logging.getLevelName(logging.ERROR):    "[{log_color}{asctime}] {name} {threadName} - {levelname}: {message} ({filename}:{lineno})",
        logging.getLevelName(logging.CRITICAL): "[{log_color}{asctime}] {name} {threadName} - {levelname}: {message} ({filename}:{lineno})",
    }, style="{")

    # Create a handler that outputs log messages to stderr
    handler = logging.StreamHandler(sys.stderr)
    handler.setFormatter(formatter)  # Apply the formatter to the handler
    logger.addHandler(handler)  # Add the configured handler to the logger

    return logger