pypeit.logger module

PypeIt logging

Implementation heavily references loggers from astropy and sdsstools.

class pypeit.logger.DebugStreamFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)[source]

Bases: StreamFormatter

Set the base logging level to DEBUG

base_level = 10

The base logging level for the class. Used to determine whether or not to include the calling frame in the log message.

class pypeit.logger.FileFormatter(fmt='%(levelname)8s | %(asctime)s | %(filename)s:%(funcName)s:%(lineno)s | %(message)s')[source]

Bases: Formatter

Custom Formatter <logging.Formatter> for the file handler.

base_fmt = '%(levelname)8s | %(asctime)s | %(filename)s:%(funcName)s:%(lineno)s | %(message)s'
class pypeit.logger.PypeItLogger(name, level=0)[source]

Bases: Logger

Custom logging system for pypeit.

This borrows heavily from implementations in astropy and sdsstools.

_excepthook(etype, value, trace)[source]

Override the default exception hook to log an error message.

_excepthook_orig = None
close_file()[source]

Explicitly close the log file.

static convert_verbosity_to_logging_level(v)[source]

Given a PypeIt “verbosity level,” return the logging level.

Parameters:

v (int) – PypeIt verbosity level (0, 1, or 2)

Returns:

Corresponding logging level

Return type:

int

Raises:

ValueError – Raised if the input verbosity level is not 0, 1, or 2.

init(level=20, stream=None, log_file=None, log_file_level=None)[source]

Initialise the logger.

Parameters:
  • level (int) – The logging level printed to the console

  • stream (TextIOBase | None) – Stream for logging messages, which defaults to sys.stderr.

  • log_file (str | Path | None) – Name for a log file. If None, logging is only recorded to the console. If the file provided already exists, it will be overwritten!

  • log_file_level (int | None) – The logging level specific to the log file. If None, adopt the console logging level.

makeRecord(name, level, pathname, lineno, msg, args, exc_info, func=None, extra=None, sinfo=None)[source]

Override the default makeRecord function to rework the message for exceptions.

class pypeit.logger.StreamFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)[source]

Bases: Formatter

Custom Formatter <logging.Formatter> for the stream handler.

base_level = None

The base logging level for the class. Used to determine whether or not to include the calling frame in the log message.

format(record)[source]

Format the specified record as text.

The record’s attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.

pypeit.logger.clear_text_color(text)[source]

Remove escape characters that colorize the text in a string.

Parameters:

text (str) – String to alter

Return type:

str

Returns:

String with all color escape characters removed

pypeit.logger.color_text(text, color, bold=False, nchar=None)[source]

Return an input string with escape characters to colorize text written to consoles.

Parameters:
  • text (str) – Text to colorize

  • color (list[int]) – 3-element list of integers with the RGB color values

  • bold (bool) – Flag to make the text bold

  • nchar (int | None) – Force the output text to be right-justified with this number of characters

Return type:

str

Returns:

Reformatted string

pypeit.logger.get_logger(level=20, stream=None, log_file=None, log_file_level=None)[source]

Instantiate a new logger.

Parameters:
  • level (int) – The logging level printed to the console

  • stream (TextIOBase | None) – Stream for logging messages, which defaults to sys.stderr.

  • log_file (str | Path | None) – Name for a log file. If None, logging is only recorded to the console. If the file provided already exists, it will be ovewritten!

  • log_file_level (int | None) – The logging level specific to the log file. If None, adopt the console logging level.

Return type:

PypeItLogger

Returns:

Logging object for PypeIt.

pypeit.logger.short_warning(message, category, filename, lineno, line=None)[source]

Overrides default formatting of warning messages. The only arguments used are message and category. See warnings.formatwarning().