AstropyLogger

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

Bases: 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.

Initialize the logger with a name and an optional level.

Methods Summary

disable_color()

Disable colorized output

disable_exception_logging()

Disable logging of exceptions

disable_warnings_logging()

Disable logging of warnings.warn() calls

enable_color()

Enable colorized output

enable_exception_logging()

Enable logging of exceptions

enable_warnings_logging()

Enable logging of warnings.warn() calls

exception_logging_enabled()

Determine if the exception-logging mechanism is enabled.

log_to_file(filename[, filter_level, ...])

Context manager to temporarily log messages to a file.

log_to_list([filter_level, filter_origin])

Context manager to temporarily log messages to a list.

makeRecord(name, level, pathname, lineno, ...)

A factory method which can be overridden in subclasses to create specialized LogRecords.

warnings_logging_enabled()

Methods Documentation

disable_color()[source]

Disable colorized output

disable_exception_logging()[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.

disable_warnings_logging()[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.

enable_color()[source]

Enable colorized output

enable_exception_logging()[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.

enable_warnings_logging()[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.

exception_logging_enabled()[source]

Determine if the exception-logging mechanism is enabled.

Returns:
exclogbool

True if exception logging is on, False if not.

log_to_file(filename, filter_level=None, filter_origin=None)[source]

Context manager to temporarily log messages to a file.

Parameters:
filenamepython:str

The file to log messages to.

filter_levelpython: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_originpython: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(filter_level=None, filter_origin=None)[source]

Context manager to temporarily log messages to a list.

Parameters:
filenamepython:str

The file to log messages to.

filter_levelpython: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_originpython: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]

A factory method which can be overridden in subclasses to create specialized LogRecords.

warnings_logging_enabled()[source]