AstropyLogger

class astropy.logger.AstropyLogger(name, level=0) [edit on github][source]

Bases: logging.Logger

This class is used to set up the Astropy logging.

The main functionality added by this class over the built-in logging.Logger class is the ability to keep track of the origin of the messages, the ability to enable logging of warnings.warn calls and exceptions, and the addition of colorized output and context managers to easily capture messages to a file or list.

Methods Summary

warnings_logging_enabled()
exception_logging_enabled() Determine if the exception-logging mechanism is enabled.
log_to_file(*args, **kwds) Context manager to temporarily log messages to a file.
disable_color() Disable colorized output
setLevel(level) Set the logging level of this logger.
enable_warnings_logging() Enable logging of warnings.warn() calls Once called, any subsequent calls to warnings.warn() are redirected to this logger and emitted with level WARN.
enable_color() Enable colorized output
enable_exception_logging() Enable logging of exceptions Once called, any uncaught exceptions will be emitted with level ERROR by this logger, before being raised.
disable_warnings_logging() Disable logging of warnings.warn() calls Once called, any subsequent calls to warnings.warn() are no longer redirected to this logger.
disable_exception_logging() Disable logging of exceptions Once called, any uncaught exceptions will no longer be emitted by this logger.
log_to_list(*args, **kwds) Context manager to temporarily log messages to a list.
makeRecord(name, level, pathname, lineno, ...)

Methods Documentation

warnings_logging_enabled() [edit on github][source]
exception_logging_enabled() [edit on github][source]

Determine if the exception-logging mechanism is enabled.

Returns :

exclog : bool

True if exception logging is on, False if not.

log_to_file(*args, **kwds) [edit on github][source]

Context manager to temporarily log messages to a file.

Parameters :

filename : str

The file to log messages to.

filter_level : str

If set, any log messages less important than filter_level will not be output to the file. Note that this is in addition to the top-level filtering for the logger, so if the logger has level ‘INFO’, then setting filter_level to INFO or DEBUG will have no effect, since these messages are already filtered out.

filter_origin : str

If set, only log messages with an origin starting with filter_origin will be output to the file.

Notes

By default, the logger already outputs log messages to a file set in the Astropy configuration file. Using this context manager does not stop log messages from being output to that file, nor does it stop log messages from being printed to standard output.

Examples

The context manager is used as:

with logger.log_to_file('myfile.log'):
    # your code here
disable_color() [edit on github][source]

Disable colorized output

setLevel(level) [edit on github][source]

Set the logging level of this logger.

enable_warnings_logging() [edit on github][source]

Enable logging of warnings.warn() calls

Once called, any subsequent calls to warnings.warn() are redirected to this logger and emitted with level WARN. Note that this replaces the output from warnings.warn.

This can be disabled with disable_warnings_logging.

enable_color() [edit on github][source]

Enable colorized output

enable_exception_logging() [edit on github][source]

Enable logging of exceptions

Once called, any uncaught exceptions will be emitted with level ERROR by this logger, before being raised.

This can be disabled with disable_exception_logging.

disable_warnings_logging() [edit on github][source]

Disable logging of warnings.warn() calls

Once called, any subsequent calls to warnings.warn() are no longer redirected to this logger.

This can be re-enabled with enable_warnings_logging.

disable_exception_logging() [edit on github][source]

Disable logging of exceptions

Once called, any uncaught exceptions will no longer be emitted by this logger.

This can be re-enabled with enable_exception_logging.

log_to_list(*args, **kwds) [edit on github][source]

Context manager to temporarily log messages to a list.

Parameters :

filename : str

The file to log messages to.

filter_level : str

If set, any log messages less important than filter_level will not be output to the file. Note that this is in addition to the top-level filtering for the logger, so if the logger has level ‘INFO’, then setting filter_level to INFO or DEBUG will have no effect, since these messages are already filtered out.

filter_origin : str

If set, only log messages with an origin starting with filter_origin will be output to the file.

Notes

Using this context manager does not stop log messages from being output to standard output.

Examples

The context manager is used as:

with logger.log_to_list() as log_list:
    # your code here
makeRecord(name, level, pathname, lineno, msg, args, exc_info, func=None, extra=None, sinfo=None) [edit on github][source]

Page Contents