Skip to main content

The package wraps the built-in Logger package. There are options to track from where the log message was created.

Project description

LoggerWrapper Package

This package provides wrappers for the built-in logging.Logger class:

PseudoSingletonLogger::

The PseudoSingletonLogger class inherits the logging.Logger and instantiates a singleton for each logger name give.
PsudoSigletonLogger pre-configures some of the common tasks like formating to help a standardize logging format.
If given the optional 'app_name' the name of the application is placed in the header.
If given the 'use_instance' flag a space is allocated for the instance name for the client class to inject during logging.
If given the optional 'name' string the name is used to find the logger by that name, otherwise the 'root' logger is used.
Each unique 'name' creates a new instance. Setting the name equal to None will use the previously used instance. Using a previously used name will return that instance.

The PsudoSigletonLogger class has the following methods::

set_default_format(logger_name: str = None, app_name: str = None, use_instance: bool = False):
Sets the default logging format for the given logger_name. If no logger_name than the default is the last_logger instance used.

get_output_path(logger_name: str = None, handler_type: logging.Handler=None):
Tries to retrieve a list of output targets of the handler that matches the type passed in. If handler_type is None, all the output targets for all the registered handlers are returned.
If no logger_name provided than the default is the last_logger instance used.

NOTE: Not fully tested for all handler types. Tested on StreamHandler, FileHandler, and SyslogHandler

remove_handler(logger_name: str = None, handler_type: logging.Handler=None):
Removes the handlers that matches the type passed in. If the handler_type is None or the handler_type is not registered, none of the handlers will be removed. If an abstract handler is given, all handler that have inherited will be removed.
If no logger_name provided than the default is the last_logger instance used.

version:
The package version.

LoggerWrapper::

The LoggerWrapper class wraps the logging.Logger to pre-configure some of the common tasks like formating. Provides a quick access to logging by formating the messages to help stardardize the log entries. This class injects the instance name into the log messages.
If given the optional 'instance_name' string the given name is used, otherwise the instance name is extracted from the stack. The instance name is inject into the header during logging.
If given the optional 'name' string the name is used to find the logger by that name, otherwise the 'root' logger is used.

The LoggerWrapper class has the following methods::

change_instance_name(self, instance_name: str):
Changes the instance name to use in the log message header.

_log(level, msg, args, exc_info=None, extra=None, stack_info=False, stacklevel: int=1):
Overwrites the logging.Logger._log method to inject the instance name in the log message header.

LoggerWrapper also exposes the PseudoSingletonLogger methods as its own.

Example Usage ::

import logging
from logging import handlers as hdls
from pathlib import Path

if __name__ == "__main__":
    log_path = Path(".logs", "test.log")
    if not log_path.parent.exists():
        log_path.parent.mkdir(parents=True, exist_ok=True)
    handlers = [logging.StreamHandler(),
                logging.FileHandler(log_path)]
    log1 = LoggerWrapper(app_name="LoggerWrapper Demo",
                         handlers=handlers,
                         meta=True,
                         date_filename=False)

    log2 = LoggerWrapper()
    print(log2.version)

    print(str(log1.get_output_path()))

    for count in range(10):
        if count == 5:
            log1.remove_handler(logging.StreamHandler)
        log1.info("test of " + str(count))
        log2.info("test of " + str(count))

    print(str(log2.get_output_path()))
    print(log1.version)

Console ::

0.1.0
['', '/home/erol/workspace/logger-wrapper/.logs/test.log']
2023-05-03 23:15:41,123,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log1:logger_wrapper::408],test of 0
2023-05-03 23:15:44,484,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log2:logger_wrapper::409],test of 0
2023-05-03 23:15:48,254,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log1:logger_wrapper::408],test of 1
2023-05-03 23:15:49,052,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log2:logger_wrapper::409],test of 1
2023-05-03 23:15:51,307,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log1:logger_wrapper::408],test of 2
2023-05-03 23:15:51,904,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log2:logger_wrapper::409],test of 2
2023-05-03 23:15:53,822,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log1:logger_wrapper::408],test of 3
2023-05-03 23:15:54,505,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log2:logger_wrapper::409],test of 3
2023-05-03 23:15:56,481,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log1:logger_wrapper::408],test of 4
2023-05-03 23:15:57,099,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log2:logger_wrapper::409],test of 4
['/home/erol/workspace/logger-wrapper/.logs/test.log']
0.1.0

cat ~/workspace/logger-wrapper/.logs/test.log ::

2023-05-03 23:15:41,123,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log1:logger_wrapper::408],test of 0
2023-05-03 23:15:44,484,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log2:logger_wrapper::409],test of 0
2023-05-03 23:15:48,254,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log1:logger_wrapper::408],test of 1
2023-05-03 23:15:49,052,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log2:logger_wrapper::409],test of 1
2023-05-03 23:15:51,307,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log1:logger_wrapper::408],test of 2
2023-05-03 23:15:51,904,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log2:logger_wrapper::409],test of 2
2023-05-03 23:15:53,822,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log1:logger_wrapper::408],test of 3
2023-05-03 23:15:54,505,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log2:logger_wrapper::409],test of 3
2023-05-03 23:15:56,481,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log1:logger_wrapper::408],test of 4
2023-05-03 23:15:57,099,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log2:logger_wrapper::409],test of 4
2023-05-03 23:16:01,619,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log1:logger_wrapper::408],test of 5
2023-05-03 23:16:02,369,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log2:logger_wrapper::409],test of 5
2023-05-03 23:16:04,266,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log1:logger_wrapper::408],test of 6
2023-05-03 23:16:04,763,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log2:logger_wrapper::409],test of 6
2023-05-03 23:16:06,621,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log1:logger_wrapper::408],test of 7
2023-05-03 23:16:07,148,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log2:logger_wrapper::409],test of 7
2023-05-03 23:16:09,007,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log1:logger_wrapper::408],test of 8
2023-05-03 23:16:09,595,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log2:logger_wrapper::409],test of 8
2023-05-03 23:16:12,300,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log1:logger_wrapper::408],test of 9
2023-05-03 23:16:12,301,LoggerWrapper Demo,[INFO:pid=3356970:MainThread:log2:logger_wrapper::409],test of 9

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

logger_wrapper-0.1.0.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

logger_wrapper-0.1.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file logger_wrapper-0.1.0.tar.gz.

File metadata

  • Download URL: logger_wrapper-0.1.0.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for logger_wrapper-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9e22cb406d814e7f4272e527fb8ce1622c4d8a334d72e9038b7a50d5b2998948
MD5 3572ab9e9484528c62277c711fe07f2c
BLAKE2b-256 2ea5be822f3c87b5a198f8ce854b8573f94ae7a8d6473c20d98afa4f9d881389

See more details on using hashes here.

File details

Details for the file logger_wrapper-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for logger_wrapper-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ca8b87a20300bed4dbb8fb802a2c5886eb424e68535f0ba88076857c0c800c83
MD5 76db16e8e9b8b4c471c785080a73a25e
BLAKE2b-256 a69a15c41da8a0eb180c54165148b8b8ddfb1ef72b12e2fafa83e5333ca80489

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page