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 colorized output
Disable logging of exceptions
Disable logging of warnings.warn() calls
Enable colorized output
Enable logging of exceptions
Enable logging of warnings.warn() calls
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.
Methods Documentation
- 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_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 levelWARN
. Note that this replaces the output fromwarnings.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:
- filename
python:str
The file to log messages to.
- filter_level
python: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 settingfilter_level
toINFO
orDEBUG
will have no effect, since these messages are already filtered out.- filter_origin
python:str
If set, only log messages with an origin starting with
filter_origin
will be output to the file.
- filename
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:
- filename
python:str
The file to log messages to.
- filter_level
python: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 settingfilter_level
toINFO
orDEBUG
will have no effect, since these messages are already filtered out.- filter_origin
python:str
If set, only log messages with an origin starting with
filter_origin
will be output to the file.
- filename
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