SEPLogger¶
- Author
Marcel Simader
- Date
12.05.2021
New in version v0.1.2.
Logger Levels¶
- class SEPModules.SEPLogger.LevelType(value)¶
Enum for indicating which type of Level an enum entry in a
Levelsubclass is. Possible values are:NORMALERRORDEBUG
- class SEPModules.SEPLogger.Level(value)¶
Abstract base class for all
Loggerlevel enums.Loggervalues are comparable using the standard compare operators and additionally must be instantiated with a tuple of form(level, prefix, style, level_type)with typesint,AnyStr,SEPModules.SEPPrinting.AnsiControl, andLevelTypewhich can be accessed as properties of the instances of this class.- final classmethod get_errors()¶
This function provides those levels which represent an error so that the
Loggerclass can discern between normal outputs and error outputs.- Returns
an iterator of
Levelenum entry which represent the error levels- Return type
Tuple[SEPModules.SEPLogger.Level, …]
- final classmethod get_debug()¶
This function provides the debug enum for use in the
Loggerclass. This function is needed to provide the logger class with a debug level.- Returns
an iterator of
Levelenum entry which represents the debug levels- Return type
Tuple[SEPModules.SEPLogger.Level, …]
Logger¶
- class SEPModules.SEPLogger.Logger(level_class=DefaultLevel, min_level=DefaultLevel.VERBOSE, level_mask=None, print_function=None, use_timestamp=False, use_date=False, ignore_level_restrictions=False, use_color=True)¶
Loggerprovides a convenient way to handle printing to the console, or to another stream or file.A logger instance holds configurations on how to print data and what additional meta-data to include. The
Levelenum can be used to configure different levels of verbosity (e.g. WARNING, ERROR, etc.) with default levels already implemented inDefaultLevels, which are also accessible through their aliasesVERBOSE,INFO,WARNING,ERROR,FATAL_ERROR. Logger also includes a convenience methoddebug()which can be used to debug and includes additional meta-data such as the file name and line number.For convenience, one might want to set up the logger as a constant, or use the provided
DEFAULT_LOGGERinstance in the following fashion:>>> from SEPModules.SEPLogger import DEFAULT_LOGGER >>> from SEPModules import log # alias to the log method is already provided! >>> from SEPModules import debug # and so is an alias to the debug method! >>> fatal_error = lambda *msg: DEFAULT_LOGGER.log(*msg, level=FATAL_ERROR) # alias to an error output of the log function
Now we can simply use the aliases to access the logger:
log("this is an error", level=ERROR) [Error] [<Timestamp>] this is an error debug(("_test", "tuple"), 123) [Debug] [<Timestamp>] [<input>::<function>:<lineno>] ('_test', 'tuple') 123 fatal_error("halting problem is not decidable!") [Fatal Error] [<Timestamp>] halting problem is not decidable!- Parameters
level_class (Type[LevelEnum]) – which
Levelsubclass to use as enum for the verbosity levels of this instancemin_level (LevelEnum) – the minimum level an entry must have to be printed or written to the log
level_mask (Optional[Container[LevelEnum]]) – a container of levels to ignore when printing or writing to the log
out (TextIO) – the default out TextIO stream to print to
err (TextIO) – the default error TextIO stream to print to (may be the same as
out, if desired)use_timestamp (bool) – whether or not to print a timestamp with each log entry (see
_provide_timestamp())use_date (bool) – whether or not to add the current date to each timestamp
ignore_level_restrictions (bool) – if set to True, this will ignore the values of the
min_levelandlevel_maskparametersuse_color (bool) – whether or not to use color when printing or writing to the log
- _provide_timestamp()¶
Provides the current time as timestamp.
- Returns
a timestamp in the format
DAY.MONTH.YEAR HOUR:MINUTE:SECOND.MILLISECONDwith the date only appearing ifuse_dateis True, ifuse_timestampis False this method will simply return the empty string- Return type
str
- final _local_print(msg, level)¶
Prints the message with appropriate level to the appropriate defined output stream (see
out, anderr). This methods also performs various system-dependent checks and decides whether to use color or not, and what encoding to use.This method considers any levels marked as
debugto be normal output, even if they also occur aserror.Warning
This function only uses color output if the given stream’s
isatty()method returnsTrue, along with some other platform-dependent checks.- Raises
UnicodeError – if no encoding attempt was successful
- property min_level: SEPModules.SEPLogger.LevelEnum¶
- Returns
the minimum level to be logged by this instance
- property level_mask: Optional[Collection[SEPModules.SEPLogger.LevelEnum]]¶
- Returns
the level mask blocking specific levels from being logged
- property level_class: Type[SEPModules.SEPLogger.LevelEnum]¶
- Returns
the enum of which all levels used by this instance must be a part of
- log(*msg, level=DefaultLevel.INFO, start='', joiner=' ', end='\n')¶
Registers a log entry with this instance.
- Parameters
msg (Any) – the message to log, can be any number of objects
level (SEPModules.SEPLogger.LevelEnum) – which level to mark this entry as (see
LoggerandLevel)start (AnyStr) – the starting character of the message of the log entry (inserted after prefix and timestamp)
joiner (AnyStr) – the character used to join multiple input objects together (akin to
str.join)end (AnyStr) – the ending character of the log entry, similar to the end keyword argument of the
printbuiltin
- Raises
TypeError – if level is not part of the enum specified by level_class (see
Loggerconstructor)
- newline(level=DefaultLevel.INFO)¶
Registers an empty line with this instance.
- Parameters
level (SEPModules.SEPLogger.LevelEnum) – the level to mark this blank line as, this does not print a style or prefix and simply exists so that newlines can be printed conditionally based on what the level requirements of this instance are
- Raises
TypeError – if level is not part of the enum specified by level_class (see
Loggerconstructor)
- debug(*msg, stack_depth=1, include_function_name=True, start='', joiner=' ', end='\n')¶
Registers a debug log entry with this instance.
- Parameters
msg (Any) – the message to log, can be any number of objects
stack_depth (int) – how far back into the stack to trace the origin of this call
include_function_name (bool) – whether to include the name of the function scope of the call or not
start (AnyStr) – the starting character of the message of the log entry (inserted after prefix and timestamp)
joiner (AnyStr) – the character used to join multiple input objects together (akin to
str.join)end (AnyStr) – the ending character of the log entry, similar to the end keyword argument of the
printbuiltin
- @log_call(*msg, level=DefaultLevel.INFO, use_qualified_name=True, include_arguments=False, exclude_self=True, start='', joiner=' ', end='\n')¶
Customizable decorator for logging a function call with a message and a level. This log call will automatically include the function name in the output.
- Parameters
msg – the message to log, can be any number of objects
level – which level to mark this entry as (see
LoggerandLevel)use_qualified_name – whether or not to use the fully qualified name of the function in the log output
include_arguments – whether or not to include the arguments passed to the function in the log output
exclude_self – whether or not to exclude the
clsargument for methodsstart – the starting character of the message of the log entry (inserted after prefix and timestamp)
joiner – the character used to join multiple input objects together (akin to
str.join)end – the ending character of the log entry, similar to the end keyword argument of the
printbuiltin
- Returns
the customized decorator function
- @log_class(*msg, level=DefaultLevel.INFO, allow_public=True, allow_private=True, include_arguments=False, exclude_self=True, start='', joiner=' ', end='\n')¶
Customizable decorator for logging a function call with a message and a level. This log call will automatically include the function name in the output. DOCS change this docstring
Note
The
__str__and__repr__methods cannot be logged, since they maybe be used to generate the log output. The__new__method is also not logged.- Parameters
msg – the message to log, can be any number of objects
level – which level to mark this entry as (see
LoggerandLevel)allow_public – whether or not to allow public methods (all methods with names not starting with any underscores), for example
messageallow_private – whether or not to allow “sunder” or “dunder” methods (all methods with names starting with underscores), for example
_calc_runtimeinclude_arguments – whether or not to include the arguments passed to the function in the log output
exclude_self – whether or not to exclude the
clsargument for methodsstart – the starting character of the message of the log entry (inserted after prefix and timestamp)
joiner – the character used to join multiple input objects together (akin to
str.join)end – the ending character of the log entry, similar to the end keyword argument of the
printbuiltin
- Returns
the customized decorator function