gemseo / utils

Hide inherited members

logging_tools module

Logging tools.

class gemseo.utils.logging_tools.LoggingContext(logger, level=30, handler=None, close=True)[source]

Bases: object

Context manager for selective logging.

Change the level of the logger in a with block.

Examples

>>> import logging
>>> logger = logging.getLogger()
>>> logger.setLevel(logging.INFO)
>>> logger.info("This should appear.")
>>> with LoggingContext(logger):
>>>    logger.warning("This should appear.")
>>>    logger.info("This should not appear.")
>>>
>>> logger.info("This should appear.")

Source: Logging Cookbook

Parameters:
  • logger (Logger) – The logger.

  • level (int | None) –

    The level of the logger to be used on block entry. If None, do not change the level of the logger.

    By default it is set to 30.

  • handler (StreamHandler | None) – An additional handler to be used on block entry.

  • close (bool) –

    Whether to close the handler on block exit.

    By default it is set to True.

close: bool

Whether to close the handler on block exit.

handler: StreamHandler

An additional handler to be used on block entry.

level: int | None

The level of the logger to be used on block entry.

If None, do not change the level of the logger.

logger: Logger

The logger.

class gemseo.utils.logging_tools.LoggingSettings(date_format='%H:%M:%S', message_format='%(levelname)8s - %(asctime)s: %(message)s', logger=<Logger gemseo (INFO)>)[source]

Bases: object

The settings of a logger.

Parameters:
  • date_format (str) –

    By default it is set to “%H:%M:%S”.

  • message_format (str) –

    By default it is set to “%(levelname)8s - %(asctime)s: %(message)s”.

  • logger (Logger) –

    By default it is set to <Logger gemseo (INFO)>.

date_format: str = '%H:%M:%S'

The format of the date of the logged message.

logger: Logger = <Logger gemseo (INFO)>

The logger.

message_format: str = '%(levelname)8s - %(asctime)s: %(message)s'

The format of the logged message.

class gemseo.utils.logging_tools.MultiLineFileHandler(filename, mode='a', encoding=None, delay=False, errors=None)[source]

Bases: MultiLineHandlerMixin, FileHandler

FileHandler to split multiline logging messages.

Open the specified file and use it as the stream for logging.

acquire()

Acquire the I/O thread lock.

addFilter(filter)

Add the specified filter to this handler.

close()

Closes the stream.

createLock()

Acquire a thread lock for serializing access to the underlying I/O.

emit(record)

Emit one logging message per input record line.

Return type:

None

filter(record)

Determine if a record is loggable by consulting all the filters.

The default is to allow the record to be logged; any filter can veto this and the record is then dropped. Returns a zero value if a record is to be dropped, else non-zero.

Changed in version 3.2: Allow filters to be just callables.

flush()

Flushes the stream.

format(record)

Format the specified record.

If a formatter is set, use it. Otherwise, use the default formatter for the module.

get_name()
handle(record)

Conditionally emit the specified logging record.

Emission depends on filters which may have been added to the handler. Wrap the actual emission of the record with acquisition/release of the I/O thread lock. Returns whether the filter passed the record for emission.

handleError(record)

Handle errors which occur during an emit() call.

This method should be called from handlers when an exception is encountered during an emit() call. If raiseExceptions is false, exceptions get silently ignored. This is what is mostly wanted for a logging system - most users will not care about errors in the logging system, they are more interested in application errors. You could, however, replace this with a custom handler if you wish. The record which was being processed is passed in to this method.

release()

Release the I/O thread lock.

removeFilter(filter)

Remove the specified filter from this handler.

setFormatter(fmt)

Set the formatter for this handler.

setLevel(level)

Set the logging level of this handler. level must be an int or a str.

setStream(stream)

Sets the StreamHandler’s stream to the specified value, if it is different.

Returns the old stream, if the stream was changed, or None if it wasn’t.

set_name(name)
property name
terminator = '\n'
class gemseo.utils.logging_tools.MultiLineHandlerMixin[source]

Bases: object

Stateless mixin class to override logging handlers behavior.

emit(record)[source]

Emit one logging message per input record line.

Return type:

None

class gemseo.utils.logging_tools.MultiLineStreamHandler(stream=None)[source]

Bases: MultiLineHandlerMixin, StreamHandler

StreamHandler to split multiline logging messages.

Initialize the handler.

If stream is not specified, sys.stderr is used.

acquire()

Acquire the I/O thread lock.

addFilter(filter)

Add the specified filter to this handler.

close()

Tidy up any resources used by the handler.

This version removes the handler from an internal map of handlers, _handlers, which is used for handler lookup by name. Subclasses should ensure that this gets called from overridden close() methods.

createLock()

Acquire a thread lock for serializing access to the underlying I/O.

emit(record)

Emit one logging message per input record line.

Return type:

None

filter(record)

Determine if a record is loggable by consulting all the filters.

The default is to allow the record to be logged; any filter can veto this and the record is then dropped. Returns a zero value if a record is to be dropped, else non-zero.

Changed in version 3.2: Allow filters to be just callables.

flush()

Flushes the stream.

format(record)

Format the specified record.

If a formatter is set, use it. Otherwise, use the default formatter for the module.

get_name()
handle(record)

Conditionally emit the specified logging record.

Emission depends on filters which may have been added to the handler. Wrap the actual emission of the record with acquisition/release of the I/O thread lock. Returns whether the filter passed the record for emission.

handleError(record)

Handle errors which occur during an emit() call.

This method should be called from handlers when an exception is encountered during an emit() call. If raiseExceptions is false, exceptions get silently ignored. This is what is mostly wanted for a logging system - most users will not care about errors in the logging system, they are more interested in application errors. You could, however, replace this with a custom handler if you wish. The record which was being processed is passed in to this method.

release()

Release the I/O thread lock.

removeFilter(filter)

Remove the specified filter from this handler.

setFormatter(fmt)

Set the formatter for this handler.

setLevel(level)

Set the logging level of this handler. level must be an int or a str.

setStream(stream)

Sets the StreamHandler’s stream to the specified value, if it is different.

Returns the old stream, if the stream was changed, or None if it wasn’t.

set_name(name)
property name
terminator = '\n'
class gemseo.utils.logging_tools.OneLineLogging(logger)[source]

Bases: object

A context manager to make the StreamHandlers use only one line.

Each record replaces the previous one.

Parameters:

logger (Logger) – The logger.

gemseo.utils.logging_tools.DEFAULT_DATE_FORMAT: Final[str] = '%H:%M:%S'

The format of the date of the logged message.

gemseo.utils.logging_tools.DEFAULT_MESSAGE_FORMAT: Final[str] = '%(levelname)8s - %(asctime)s: %(message)s'

The format of the logged message.

gemseo.utils.logging_tools.GEMSEO_LOGGER: Final[Logger] = <Logger gemseo (INFO)>

The GEMSEO’s logger.

gemseo.utils.logging_tools.LOGGING_SETTINGS = LoggingSettings(date_format='%H:%M:%S', message_format='%(levelname)8s - %(asctime)s: %(message)s', logger=<RootLogger root (INFO)>)

The logging settings.

The parameters are changed by configure_logger().