logseg.log_setup

Classes

LoggerManager

This class manages the logger thread.

RedirectToLogger

Used to redirect stdout and stderr to logger in _redirect_stdout_stderr

CreateFileHandlerHandler

This class creates a file handler for a logger instance.

Functions

_add_file_handler(→ None)

This function adds a file handler to a logger instance.

_get_log_formatter(config)

This function gets the log formatter with timezone support.

_get_root_logger([config])

This function gets the root logger and sets its level based on configuration.

_redirect_stdout_stderr(→ None)

This function redirects the standard out and standard error to logger instances.

_configure_logging_handlers(→ logging.Logger)

This function configures the logging handlers for the logger instance.

_lt(queue)

This function acts as the thread that listens to the logger queue and sends queued logs to the logger instance.

logger_init(→ LoggerManager)

This function initializes a logger as well as a thread to process logs produced by concurrent processes. Logs

get_logger(→ logging.Logger)

This function gets a logger instance.

Module Contents

class logseg.log_setup.LoggerManager(logger_thread: threading.Thread)

This class manages the logger thread.

Parameters:

logger_thread – The logger thread to manage.

Returns:

logger_thread
terminate_logger()

This method terminates the logger thread. It should be called when log is complete during program cleanup.

Returns:

class logseg.log_setup.RedirectToLogger(logger, log_level=logging.INFO)

Bases: object

Used to redirect stdout and stderr to logger in _redirect_stdout_stderr

Parameters:
  • logger – The logger instance to redirect to.

  • log_level – The log level to use for the logger.

Returns:

logger
log_level = 20
value = None
write(message)

This method writes a message to the logger instance.

Parameters:

message – The message to write to the logger instance.

Returns:

flush()
getvalue()
class logseg.log_setup.CreateFileHandlerHandler(config: configparser.ConfigParser)

Bases: logging.Handler

This class creates a file handler for a logger instance.

Parameters:

config – A ConfigParser containing the logger configuration.

Returns:

config
segregate_regex
seg_name_regex
_process_logseg(log_str: str) Tuple[str, str]

This method processes a logseg log record.

Parameters:

log_str – The log string to be processed.

Returns:

A Tuple containing the final message and the segregate folder name for the log string.

emit(record)

This method emits a log record to the logger instance.

Parameters:

record – The log record to emit.

Returns:

logseg.log_setup._add_file_handler(config: configparser.ConfigParser, instance: logging.Logger, log_formatter: logging.Formatter, folder_name: str | None = None) None

This function adds a file handler to a logger instance.

Parameters:
  • config – A ConfigParser containing the logger configuration.

  • instance – The logger instance to add the file handler to.

  • log_formatter – The log formatter to use for the file handler.

  • folder_name – The name of the folder to segregate logs into.

Returns:

logseg.log_setup._get_log_formatter(config: configparser.ConfigParser)

This function gets the log formatter with timezone support.

Parameters:

config – A ConfigParser containing the logger configuration.

Returns:

A logging.Formatter instance with timezone support.

logseg.log_setup._get_root_logger(config: configparser.ConfigParser = None)

This function gets the root logger and sets its level based on configuration.

Parameters:

config – A ConfigParser containing the logger configuration. If None, the default config will be used.

Returns:

The root logger with the configured log level.

logseg.log_setup._redirect_stdout_stderr(logger: logging.Logger) None

This function redirects the standard out and standard error to logger instances.

Parameters:

logger – The logger instance to redirect stdout and stderr to.

Returns:

logseg.log_setup._configure_logging_handlers(config: configparser.ConfigParser) logging.Logger

This function configures the logging handlers for the logger instance.

Parameters:

config – A ConfigParser containing the logger configuration.

Returns:

A Logger instance.

logseg.log_setup._lt(queue: multiprocessing.queues.Queue)

This function acts as the thread that listens to the logger queue and sends queued logs to the logger instance.

Parameters:

queue – A multiprocessing Queue, managed by a multiprocessing Manager.

Returns:

logseg.log_setup.logger_init(config_file: pathlib.Path | str = None) LoggerManager

This function initializes a logger as well as a thread to process logs produced by concurrent processes. Logs from concurrent processes should be passed through the multiprocessing queue stored in log.globals.logger_queue.

Parameters:
  • config_file – A path to a configuration file containing a LOGSEG section. If not provided, the default

  • file. (configuration will be used. See the documentation for an example configuration)

Returns:

A LoggerManager instance which can be used to terminate the logger thread at cleanup time.

logseg.log_setup.get_logger(name: str, queue: multiprocessing.queues.Queue | None = None) logging.Logger

This function gets a logger instance.

Setup

from logseg import LoggerManager
from logseg import logger_init

logger_manager: LoggerManager = logger_init()

Without multiprocessing

from logseg import get_logger

logger = get_logger(__name__)
logger.info('your message')

With multiprocessing

import multiprocessing as mp
import log.globals

def my_function(queue: mp.Queue, parameter: str):
    logger = get_logger(__name__, queue)
    logger.info(f'Your message: {parameter}')

pool = mp.Pool(processes=mp.cpu_count())
pool.imap_unordered(func=partial(
    my_function,
    queue=log.globals.logger_queue,
    parameter='example'
))
pool.close()
pool.join()

Shutdown

logger_manager.terminate_logger()
Parameters:
  • name – The name for the logger.

  • queue – A Queue instance generated by logger_init(), held in the variable log.globals.logger_queue

Returns:

A logseg logger instance.