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: Union[str, int] = 30, filename: Optional[str] = 'idmtools.log', console: bool = False, file_level: Union[str, int] = 'DEBUG', force: bool = False, file_log_format_str: Optional[str] = None, user_log_format_str: str = '%(message)s', use_colored_logs: bool = True, user_output: bool = True, enable_file_logging: Union[str, bool] = True)

Bases: object

Defines the config options available for idmtools logs.

level: Union[str, int] = 30

Console level

filename: Optional[str] = 'idmtools.log'

Filename for idmtools logs

console: bool = False

Toggle to enable/disable console logging

file_level: Union[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: Union[str, bool] = True

Toggle enable file logging

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

Bases: logging.handlers.RotatingFileHandler

Multi-process safe logger.

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

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: logging.LogRecord) None

Thread safe logger.

Parameters:

record – Record to handle

Returns:

None

doRollover() None

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)

Bases: logging.Handler

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

handle(record: logging.LogRecord) None

Simple log handler that prints to stdout.

Parameters:

record – Record to print

Returns:

None

idmtools.core.logging.setup_logging(logging_config: idmtools.core.logging.IdmToolsLoggingConfig) None

Set up logging.

Parameters:

logging_config – IdmToolsLoggingConfig that defines our config

Returns:

Returns None

idmtools.core.logging.setup_handlers(logging_config: idmtools.core.logging.IdmToolsLoggingConfig)

Setup Handlers for Global and user Loggers.

Parameters:

logging_config – Logging config

Returns:

FileHandler or None

idmtools.core.logging.setup_user_logger(logging_config: idmtools.core.logging.IdmToolsLoggingConfig)

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()

Setup a print based logger for user messages.

Returns:

None

idmtools.core.logging.set_file_logging(logging_config: idmtools.core.logging.IdmToolsLoggingConfig, formatter: logging.Formatter)

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: logging.Formatter, filename: str)

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()

Reset all the logging handlers by removing the root handler.

Returns:

None

idmtools.core.logging.exclude_logging_classes(items_to_exclude=None)

Exclude items from our logger by setting level to warning.

Parameters:

items_to_exclude – Items to exclude

Returns:

None