HydLogger#
- class hyd.backend.util.logger.HydLogger(class_name: str = 'Script', level: int = 20)[source]#
Custom logger wrapping around the logging.Logger class.
- Parameters:
- class_namestr
Instance of the class the logger will be used inside of
- levelint
loglevel where FATAL = 50, ERROR = 40, WARNING = 30, INFO = 20, DEBUG = 10, NOTSET = 0
Notes
It is recommended to declare a separate logger object for every class using logging. Using the directive
HydLogger(type(self).__name__, level)
inside of the__init__
function and running thesuper().__init__(level)
from a child of said class provides a separate logger instance for each class as intended. Given the following definitions:>>> class Parent: ... def __init__(self, level: Optional[int] = logging.INFO): ... self.__log = HydLogger(type(self).__name__, level) ... ... @property ... def log(self): ... return self.__log ... >>> class Child(Parent): ... def __init__(self, level: Optional[int] = logging.INFO): ... super().__init__(level) ... >>> def main(): ... parent = Parent() ... parent.log.info("foo") ... parent.log.warning("bar") ... ... child = Child(logging.WARNING) ... child.log.warning("foo")
Finally
>>> main()
Generates an output similar to:
[2021-04-20 14:18:48][INFO][Parent][main] foo [2021-04-20 14:18:48][WARNING][Parent][main] bar [2021-04-20 14:18:48][WARNING][Child][main] foo
Methods
__init__
([class_name, level])Initialize the adapter with a logger and a dict-like object which provides contextual information.
critical
(msg, *args, **kwargs)Delegate a critical call to the underlying logger.
debug
(msg, *args, **kwargs)Delegate a debug call to the underlying logger.
error
(msg, *args, **kwargs)Delegate an error call to the underlying logger.
exception
(msg, *args[, exc_info])Delegate an exception call to the underlying logger.
getEffectiveLevel
()Get the effective level for the underlying logger.
hasHandlers
()See if the underlying logger has any handlers.
info
(msg, *args, **kwargs)Delegate an info call to the underlying logger.
isEnabledFor
(level)Is this logger enabled for level 'level'?
log
(level, msg, *args, **kwargs)Delegate a log call to the underlying logger, after adding contextual information from this adapter instance.
process
(msg, kwargs)Process the logging message and keyword arguments passed in to a logging call to insert contextual information.
setLevel
([level])Set the specified level on the underlying logger.
warn
(msg, *args, **kwargs)warning
(msg, *args, **kwargs)Delegate a warning call to the underlying logger.
Attributes
manager
name
- process(msg, kwargs)[source]#
Process the logging message and keyword arguments passed in to a logging call to insert contextual information. You can either manipulate the message itself, the keyword args or both. Return the message and kwargs modified (or not) to suit your needs.
Normally, you’ll only need to override this one method in a LoggerAdapter subclass for your specific needs.