Logging in Kerch
Many classes throughout this package display various messages during instantiation and usage. This abstract from
which they descend allows the dislpaying of those messages and control which are printed using the logging_level
attribute.
Functionalities
This abstract class has only one purpose: adding a _logger attribute meant to log various
messages across the package.
Doing it this way allows to get the name of the class instantiated and print more precise messages.
Logging Messages
The private property self._logger is an instance of the class
logging.Logger of the Python standard library logging — Logging facility for Python.
This allows logging messages by calling, e.g., self._logger.debug(message: str),
self._logger.info(message: str), self._logger.warning(message: str) or self._logger.error(message: str). The
messages will be automatically formatted to reference the appropriate class and give file and line information in debug
mode.
Logging Level
A particular log level can be set for each instance using the attribute logging_level.
If assigned to None,the default logging level will be used.
This value is also set during instantiation using the logging_level argument. If nothing is specified, None is
passed leading to the genral default logging level.
The log level always corresponds to an integer. We refer to logging — Logging facility for Python for the different values.
Default Logging Level
This is the default logging level that is used if logging_level is never specified (which corresponds to setting it to None).
By default, the logging level is 30, which corresponds to a warning level. This default package value can also be changed
and read using the following functions.
- kerch.set_logging_level(level: int)[source]
Changes the default logging level of the kerch package.
- Parameters:
level (int) – Default kerch logging value
Usage:
import kerch import logging kerch.set_logging_level(logging.DEBUG)
Warning
Changing the default logging value does not affect the already instantiated objects. We advise to set those values in the beginning of the code.
- kerch.get_logging_level() int[source]
Returns the default logging level of the kerch package.
Usage:
import kerch import logging default_level = kerch.get_logging_level() default_level = logging.getLevelName(default_level) print(default_level)
WARNING
Abstract Class
- class kerch.feature.Logger(*args, **kwargs)[source]
Bases:
object- Parameters:
logging_level (int, optional) – Logging level for this specific instance. If the value is
None, the current default kerch global log level will be used. Defaults toNone(default kerch logging level). We refer to the Logging in Kerch documentation for further information.
- property _logger: Logger
Logger of the instance.
Usage:
import kerch import logging class MyClass(kerch.feature.Logger): def __init__(self, *args, **kwargs): super(MyClass, self).__init__(*args, **kwargs) self._logger.info('Instantiation done information.') self._logger.warn('Instantiation done warning.') print('First class with default logging level:') my_class1 = MyClass() print('\nSecond instance with logging.INFO logging level:') my_class2 = MyClass(logging_level=logging.INFO)
First class with default logging level: WARNING:MyClass:Instantiation done warning. Second instance with logging.INFO logging level: INFO:MyClass:Instantiation done information. WARNING:MyClass:Instantiation done warning.
- property logging_level: int
Logging level of this specific instance. If the value is
None, the current default kerch global log Level will be used. Defaults toNone(default global kerch level). We refer to the Logging in Kerch documentation for further information.
Example
Default
import kerch
k = kerch.kernel.RBF()
k.sample = range(3)
print(k.K) # first warning (the sigma is defined)
WARNING:RBF:The kernel bandwidth sigma has not been provided and is assigned by a heuristic (sigma=5.00e-01).
tensor([[1.0000e+00, 1.3534e-01, 3.3546e-04],
[1.3534e-01, 1.0000e+00, 1.3534e-01],
[3.3546e-04, 1.3534e-01, 1.0000e+00]])
Info
import kerch
import logging
k = kerch.kernel.RBF(logging_level=logging.INFO) # first info (no sample initialized yet)
k.sample = range(3) # second info (the sample is initialized)
print(k.K) # first warning (the sigma is defined)
INFO:RBF:The sample cannot be initialized because no sample data has been provided nor the sample dimensions have been initialized yet.
WARNING:RBF:The kernel bandwidth sigma has not been provided and is assigned by a heuristic (sigma=5.00e-01).
tensor([[1.0000e+00, 1.3534e-01, 3.3546e-04],
[1.3534e-01, 1.0000e+00, 1.3534e-01],
[3.3546e-04, 1.3534e-01, 1.0000e+00]])
Inheritance
This is a base class that directly inherits from object.