SpLogger#
- class scikitplot.sp_logging.SpLogger(*args, **kwargs)[source]#
A singleton logger class that provides a shared logger instance with customizable name, formatter, handler, logging level, and thread-safety.
This class implements the Singleton pattern, ensuring only a single instance of the logger exists throughout the application. It supports different log levels (e.g., DEBUG, INFO, WARNING) and thread-safe logging.
Important
If the attribute is not defined within the
SpLogger
class, it will be dynamically fetched from thelogging
module. This is particularly useful for logging constants likeDEBUG
,INFO
,ERROR
, etc., which are often used in logging configurations but are not necessarily attributes of the logger class itself.Attention
Be cautious when using dynamically retrieved attributes, as it may lead to unexpected behavior if the module’s constants change.
- Parameters:
- nameOptional[str], default=None
The name of the logger. If None, defaults to ‘scikitplot’.
- formatterOptional[logging.Formatter], default=None
The formatter to use for logging. If None, a default customized formatter is applied.
- handlerOptional[logging.Handler], default=None
The handler to use for logging. If None, a default customized handler is applied.
- log_levelOptional[int], default=None
The logging level to set for the logger (e.g., DEBUG, INFO, WARNING). Defaults to WARNING.
- thread_safebool, default=True
Indicates whether thread-safety is enabled for the logger. If True, a threading lock is used.
- *argsAny, optional
Additional positional arguments for customization.
- **kwargsAny, optional
Additional keyword arguments for further customization.
- Parameters:
See also
get_logger
Function that provides a shared
logging.Logger
instance.sp_logger
An instance of
SpLogger
class, providing logging functionality.logging.getLogger
Standard library function to retrieve
logging.Logger
instance, for more https://docs.python.org/3/library/logging.html.
Notes
See Python documentation (https://docs.python.org/3/library/logging.html) for detailed API. Below is only a summary.
The logger has 5 levels of logging from the most serious to the least.
Here supported logging levels:
CRITICAL
,FATAL
ERROR
WARNING
INFO
DEBUG
The logger methods support string formatting. For example:
>>> import scikitplot as sp >>> sp.SpLogger().error("The value %d is invalid.", 3)
You can change the verbosity of the logger as follows:
>>> import scikitplot as sp >>> sp.SpLogger().setLevel(sp.SpLogger().INFO) # set level INFO >>> sp.SpLogger().debug("This is a debug.") # This will not be shown, as level is INFO. >>> sp.SpLogger().info("This is a info.") # This will be shown, as level is INFO. >>> sp.SpLogger().warning("This is a warning.") # This will be shown, as level is INFO.
Examples
Get a root logger by module:
>>> import scikitplot.sp_logging as logging # module logger >>> logging.setLevel(logging.INFO) # default WARNING >>> logging.info("This is a info message from the sp logger.")
2025-01-17 22:34:27 INFO scikitplot 140249475356480 sp_logging.py 820 This is a info message from the sp logger.
Get a root logger by func:
>>> from scikitplot import sp_logging, get_logger; logging=get_logger() # pure python logger, not have direct log level >>> logging.setLevel(sp_logging.INFO) # default WARNING >>> logging.info("This is a info message from the sp logger.")
2025-01-17 22:34:27 INFO scikitplot 140249475356480 1353514787.py 3 This is a info message from the sp logger.
Get a root logger by class:
>>> from scikitplot import SpLogger; logging=SpLogger() # class logger >>> logging.setLevel(logging.INFO) # default WARNING >>> logging.info("This is a info message from the sp logger.")
2025-01-17 22:34:27 INFO scikitplot 140249475356480 sp_logging.py 1146 This is a info message from the sp logger.
Get a root logger by class instance:
>>> from scikitplot import sp_logger as logging # class instance logger >>> logging.setLevel(logging.INFO) # default WARNING >>> logging.info("This is a info message from the sp logger.")
2025-01-17 22:34:27 INFO scikitplot 140249475356480 sp_logging.py 1146 This is a info message from the sp logger.
- getEffectiveLevel()[source]#
Gets the current logging level of the logger.
- Returns:
- int
The current logging level (e.g., DEBUG, INFO, WARNING) of the logger.
- Return type:
- property level: int#
Gets the current logging level of the logger.
- Returns:
- int
The current logging level (e.g., DEBUG, INFO, WARNING) of the logger.
- log_if(level, msg, condition, *args, **kwargs)[source]#
Log ‘msg % args’ at level ‘level’ only if condition is fulfilled.
- setLevel(level)[source]#
Set the logger’s logging level.
- Parameters:
- levelint
The logging level to set (e.g., DEBUG, INFO, WARNING).
- Parameters:
level (int)