idmtools.core.logging module#

idmtools logging module.

We configure our logging here, manage multi-process logging, alternate logging level, and additional utilities to manage logging.

Copyright 2021, Bill & Melinda Gates Foundation. All rights reserved.

class idmtools.core.logging.IdmToolsLoggingConfig(level: str | int = 30, filename: str | None = 'idmtools.log', console: bool = False, file_level: str | int = 'DEBUG', force: bool = False, file_log_format_str: str | None = None, user_log_format_str: str = '%(message)s', use_colored_logs: bool = True, user_output: bool = True, enable_file_logging: str | bool = True)[source]#

Bases: object

Defines the config options available for idmtools logs.

level: str | int = 30#

Console level

filename: str | None = 'idmtools.log'#

Filename for idmtools logs

console: bool = False#

Toggle to enable/disable console logging

file_level: str | int = 'DEBUG'#

File log level

force: bool = False#

Should we force reload

file_log_format_str: str = None#

//docs.python.org/3/library/logging.html#logrecord-attributes for format vars

Type:

File format string. See https

user_log_format_str: str = '%(message)s'#

//docs.python.org/3/library/logging.html#logrecord-attributes for format vars

Type:

Logging format. See https

use_colored_logs: bool = True#

Toggle to enable/disable coloredlogs

user_output: bool = True#

Toggle user output. This should only be used in certain situations like CLI’s that output JSON

enable_file_logging: str | bool = True#

Toggle enable file logging

__init__(level: str | int = 30, filename: str | None = 'idmtools.log', console: bool = False, file_level: str | int = 'DEBUG', force: bool = False, file_log_format_str: str | None = None, user_log_format_str: str = '%(message)s', use_colored_logs: bool = True, user_output: bool = True, enable_file_logging: str | bool = True) None#
class idmtools.core.logging.MultiProcessSafeRotatingFileHandler(filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=False)[source]#

Bases: RotatingFileHandler

Multi-process safe logger.

__init__(filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=False)[source]#

See RotatingFileHandler for full details on arguments.

Parameters:
  • filename – Filename to use

  • mode – Mode

  • maxBytes – Max bytes

  • backupCount – Total backups

  • encoding – Encoding

  • delay – Delay

handle(record: LogRecord) None[source]#

Thread safe logger.

Parameters:

record – Record to handle

Returns:

None

doRollover() None[source]#

Perform rollover safely.

We loop and try to move the log file. If we encounter an issue, we try to retry three times. If we failed after three times, we try a new process id appended to file name.

Returns:

None

class idmtools.core.logging.PrintHandler(level=0)[source]#

Bases: Handler

A simple print handler. Used in cases where logging fails.

handle(record: LogRecord) None[source]#

Simple log handler that prints to stdout.

Parameters:

record – Record to print

Returns:

None

idmtools.core.logging.setup_logging(logging_config: IdmToolsLoggingConfig) None[source]#

Set up logging.

Parameters:

logging_config – IdmToolsLoggingConfig that defines our config

Returns:

Returns None

idmtools.core.logging.setup_handlers(logging_config: IdmToolsLoggingConfig)[source]#

Setup Handlers for Global and user Loggers.

Parameters:

logging_config – Logging config

Returns:

FileHandler or None

idmtools.core.logging.setup_user_logger(logging_config: IdmToolsLoggingConfig)[source]#

Setup the user logger. This logger is meant for user output only.

Parameters:

logging_config – Logging config object.

Returns:

None

idmtools.core.logging.setup_user_print_logger()[source]#

Setup a print based logger for user messages.

Returns:

None

idmtools.core.logging.set_file_logging(logging_config: IdmToolsLoggingConfig, formatter: Formatter)[source]#

Set File Logging.

Parameters:
  • logging_config – Logging config object.

  • formatter – Formatter obj

Returns:

Return File handler

idmtools.core.logging.create_file_handler(file_level, formatter: Formatter, filename: str)[source]#

Create a MultiProcessSafeRotatingFileHandler for idmtools.log.

Parameters:
  • file_level – Level to log to file

  • formatter – Formatter to set on the handler

  • filename – Filename to use

Returns:

SafeRotatingFileHandler with properties provided

idmtools.core.logging.reset_logging_handlers()[source]#

Reset all the logging handlers by removing the root handler.

Returns:

None

idmtools.core.logging.exclude_logging_classes(items_to_exclude=None)[source]#

Exclude items from our logger by setting level to warning.

Parameters:

items_to_exclude – Items to exclude

Returns:

None