Skip to content

log

plantdb.commons.log Link

get_console_handler Link

get_console_handler()

Creates and configures a console handler for logging that outputs to the standard output stream.

This handler uses a specific formatter for colored log messages.

Returns:

Type Description
StreamHandler

The configured console logging handler with a colored formatter.

Source code in plantdb/commons/log.py
62
63
64
65
66
67
68
69
70
71
72
73
74
def get_console_handler():
    """Creates and configures a console handler for logging that outputs to the standard output stream.

    This handler uses a specific formatter for colored log messages.

    Returns
    -------
    logging.StreamHandler
        The configured console logging handler with a colored formatter.
    """
    console_handler = logging.StreamHandler(sys.stdout)
    console_handler.setFormatter(COLORED_FORMATTER)
    return console_handler

get_dummy_logger Link

get_dummy_logger(logger_name, log_level=DEFAULT_LOG_LEVEL)

Creates and configures a dummy logger with a console handler.

This function generates a logger instance associated with the specified name and log level. The logger is configured with a console handler to allow output of log statements to the console. It is typically used for basic logging setups where no file-based or external configurations are required.

Parameters:

Name Type Description Default
logger_name str

A name to use for the logger.

required
log_level int or str

A logging level to set for the logger. Defaults to DEFAULT_LOG_LEVEL.

DEFAULT_LOG_LEVEL

Returns:

Type Description
Logger

The configured logger instance with a console handler attached.

Source code in plantdb/commons/log.py
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
def get_dummy_logger(logger_name, log_level=DEFAULT_LOG_LEVEL):
    """Creates and configures a dummy logger with a console handler.

    This function generates a logger instance associated with the specified name and log level.
    The logger is configured with a console handler to allow output of log statements to the console.
    It is typically used for basic logging setups where no file-based or external configurations are required.

    Parameters
    ----------
    logger_name : str
        A name to use for the logger.
    log_level : int or str, optional
        A logging level to set for the logger. Defaults to ``DEFAULT_LOG_LEVEL``.

    Returns
    -------
    logging.Logger
        The configured logger instance with a console handler attached.
    """
    logger = logging.getLogger(logger_name)
    logger.setLevel(log_level)
    logger.addHandler(get_console_handler())
    return logger

get_file_handler Link

get_file_handler(log_file)

Creates and configures a file handler for logging.

This function initializes a logging file handler, sets its level, and applies a predefined formatter to it. The file handler writes log messages to the specified file in write mode. The log level determines the severity of messages that are captured by the handler.

Parameters:

Name Type Description Default
log_file str or Path

A path to the log file where log messages will be written.

required

Returns:

Type Description
FileHandler

The configured logging file handler for capturing log messages.

Source code in plantdb/commons/log.py
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
def get_file_handler(log_file):
    """Creates and configures a file handler for logging.

    This function initializes a logging file handler, sets its level, and applies a predefined formatter to it.
    The file handler writes log messages to the specified file in write mode.
    The log level determines the severity of messages that are captured by the handler.

    Parameters
    ----------
    log_file : str or pathlib.Path
        A path to the log file where log messages will be written.

    Returns
    -------
    logging.FileHandler
        The configured logging file handler for capturing log messages.
    """
    file_handler = logging.FileHandler(log_file, mode='w')
    file_handler.setFormatter(FORMATTER)
    return file_handler

get_logger Link

get_logger(logger_name, log_file=None, log_level=DEFAULT_LOG_LEVEL)

Get a logger with a specific name and log level for console output.

This function retrieves an existing logger instance with the specified name, or creates a new one if none exists.

Parameters:

Name Type Description Default
name str

A name to use for the logger. Typically derived from the module or component that generates the logs.

required
log_file str or Path

A path to the file where log messages should be written. Defaults to None, in which case no file handler is added.

None
log_level int or str

A logging level to set for the logger. Defaults to DEFAULT_LOG_LEVEL.

DEFAULT_LOG_LEVEL

Returns:

Type Description
Logger

The configured logger instance ready to log messages with the specified settings.

Source code in plantdb/commons/log.py
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
def get_logger(logger_name, log_file=None, log_level=DEFAULT_LOG_LEVEL):
    """Get a logger with a specific name and log level for console output.

    This function retrieves an existing logger instance with the specified name, or creates a new one if none exists.

    Parameters
    ----------
    name : str
        A name to use for the logger.
        Typically derived from the module or component that generates the logs.
    log_file : str or pathlib.Path, optional
        A path to the file where log messages should be written.
        Defaults to ``None``, in which case no file handler is added.
    log_level : int or str, optional
        A logging level to set for the logger. Defaults to ``DEFAULT_LOG_LEVEL``.

    Returns
    -------
    logging.Logger
        The configured logger instance ready to log messages with the specified settings.
    """
    logger_name = logger_name.split(".")[-1]
    if not logging.getLogger(logger_name).hasHandlers():
        return _get_logger(logger_name, log_file=log_file, log_level=log_level)
    return logging.getLogger(logger_name)