AstropyLogger

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

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

disable_color() Disable colorized output
disable_exception_logging() Disable logging of exceptions Once called, any uncaught exceptions will no longer be emitted by this logger.
disable_warnings_logging() Disable logging of warnings.warn() calls Once called, any subsequent calls to warnings.warn() are no longer redirected to this logger.
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.
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.
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.
log_to_list(*args, **kwds) Context manager to temporarily log messages to a list.
makeRecord(name, level, pathname, lineno, ...)
setLevel(level) Set the logging level of this logger.
warnings_logging_enabled()

Methods Documentation

disable_color()[source] [edit on github]

Disable colorized output

disable_exception_logging()[source] [edit on github]

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.

disable_warnings_logging()[source] [edit on github]

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.

enable_color()[source] [edit on github]

Enable colorized output

enable_exception_logging()[source] [edit on github]

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.

enable_warnings_logging()[source] [edit on github]

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.

exception_logging_enabled()[source] [edit on github]

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)[source] [edit on github]

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
log_to_list(*args, **kwds)[source] [edit on github]

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)[source] [edit on github]
setLevel(level)[source] [edit on github]

Set the logging level of this logger.

warnings_logging_enabled()[source] [edit on github]

Page Contents