Skip to main content

Hierarchical logging in yaml format

Project description

Hierarchical logging in yaml format.

PyPI PyPI - Python Version PyPI - License PyPI - Downloads Coveralls GitHub code size in bytes GitHub last commit DeepSource

Hierarchical logging help to group logs that are related to the same code flow.

Log style can be styled in Yaml format or in Line format.

Examples:

Output in YAML style

from nrt_logging.logger import NrtLogger
from nrt_logging.logger_manager import logger_manager
from nrt_logging.logger_stream_handlers import ManualDepthEnum

NAME_1 = 'TEST1'
NAME_2 = 'TEST2'


class Child:
    __logger: NrtLogger

    def __init__(self):
        self.__logger = logger_manager.get_logger(NAME_1)

    def child_1(self):
        self.__logger.info('Child 1')
        self.child_2()

    def child_2(self):
        self.__logger.info('Child 2')


class Parent:
    MSG_1 = 'MSG_1'
    MSG_2 = 'MSG_2'
    MSG_3 = 'MSG_3'
    INCREASE_MSG = 'INCREASE_MSG'
    DECREASE_MSG = 'DECREASE_MSG'

    __logger: NrtLogger
    __child: Child

    def __init__(self):
        self.__logger = logger_manager.get_logger(NAME_1)
        self.__child = Child()

    def a1(self):
        self.__logger.warn(self.MSG_1)
        self.__child.child_1()

    def a2_manual(self):
        self.__logger.info(self.MSG_2)
        self.__logger.increase_depth()
        self.__logger.info(self.INCREASE_MSG)
        self.__logger.decrease_depth()
        self.__logger.info(self.DECREASE_MSG)
        self.__logger.error(self.MSG_1)
        self.a1()

    def a3_manual(self):
        self.__logger.info(self.MSG_2)
        self.__logger.increase_depth()
        self.__logger.info(self.INCREASE_MSG)
        self.__logger.decrease_depth()
        self.__logger.info(self.DECREASE_MSG)
        self.__logger.error(self.MSG_3)

    def a4_manual(self):
        self.__logger.info(self.MSG_1)
        self.__logger.info(self.INCREASE_MSG, ManualDepthEnum.INCREASE)
        self.__logger.info(self.DECREASE_MSG, ManualDepthEnum.DECREASE)
        self.__logger.error(self.MSG_2)
from examples.demo_classes.demo_classes import NAME_1, Parent
from nrt_logging.logger_manager import logger_manager
from nrt_logging.logger_stream_handlers import \
    ConsoleStreamHandler, LogStyleEnum


def logging_style(log_style: LogStyleEnum):
    sh = ConsoleStreamHandler()
    sh.style = log_style
    logger = logger_manager.get_logger(NAME_1)
    logger.add_stream_handler(sh)
    p = Parent()
    p.a1()


def logging_line_style():
    logging_style(LogStyleEnum.LINE)


def logging_yaml_style():
    logging_style(LogStyleEnum.YAML)


logging_yaml_style()

Output

---
date: 2022-10-31 17:59:04.653084
log_level: WARN
path: demo_classes.py.Parent
method: a1
line_number: 38
message: MSG_1
children:
  - date: 2022-10-31 17:59:04.655071
    log_level: INFO
    path: demo_classes.py.Child
    method: child_1
    line_number: 16
    message: Child 1
    children:
      - date: 2022-10-31 17:59:04.656137
        log_level: INFO
        path: demo_classes.py.Child
        method: child_2
        line_number: 20
        message: Child 2

Output in LINE style

- log: 2022-10-31 18:16:54.033735 [WARN] [demo_classes.py.Parent.a1:38] MSG_1
  children:
    - log: 2022-10-31 18:16:54.034660 [INFO] [demo_classes.py.Child.child_1:16] Child 1
      children:
        - log: 2022-10-31 18:16:54.036723 [INFO] [demo_classes.py.Child.child_2:20] Child 2
from nrt_logging.log_level import LogLevelEnum
from nrt_logging.logger_manager import logger_manager
from nrt_logging.logger_stream_handlers import \
    ConsoleStreamHandler, LogStyleEnum


sh = ConsoleStreamHandler()
sh.log_level = LogLevelEnum.TRACE
sh.style = LogStyleEnum.LINE
logger = logger_manager.get_logger('NAME_1')
logger.add_stream_handler(sh)

logger.info('main level log')
logger.increase_depth()
logger.info('child 1')
logger.increase_depth()
logger.info('child 1_1')
logger.decrease_depth()
logger.info('child 2')
logger.decrease_depth()
logger.info('continue main level')

Output

- log: 2022-10-31 18:18:34.520544 [INFO] [manual_hierarchy_line_logging_1.py.<module>:13] main level log
  children:
    - log: 2022-10-31 18:18:34.522606 [INFO] [manual_hierarchy_line_logging_1.py.<module>:15] child 1
      children:
        - log: 2022-10-31 18:18:34.523784 [INFO] [manual_hierarchy_line_logging_1.py.<module>:17] child 1_1
- log: 2022-10-31 18:18:34.524810 [INFO] [manual_hierarchy_line_logging_1.py.<module>:19] child 2
- log: 2022-10-31 18:18:34.525864 [INFO] [manual_hierarchy_line_logging_1.py.<module>:21] continue main level

Config file

log_manager config file in YAML style.
Configure loggers and stream handlers.

parameters are inherited. Parameters that are deeper in YAML file will be taken.

log_level: WARN
date_format: '%Y-%m-%d %H:%M:%S'
loggers:
  - name: TEST1
    style: yaml
    log_line_template: '[$log_level$] <$date$> $message$'
    stream_handlers:
      - type: console
        style: line
      - type: file
        file_path: logs/log_test_1.txt
        log_level: DEBUG
        style: line
        date_format: '%Y'
        log_line_template: 'Test1 $date$ $message$'
  - name: TEST2
    style: yaml
    stream_handlers:
      - type: file
        file_path: logs/log_test_2.txt
        log_level: ERROR
        date_format: '%Y'
        log_yaml_elements:
          ['log_level', 'date', 'message']
from nrt_logging.logger_manager import logger_manager


CONFIG_FILE_PATH = './config/config1.yaml'

logger_manager.set_config(file_path=CONFIG_FILE_PATH)

Wiki: https://github.com/etuzon/Python-NRT-Logging/wiki

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

nrt-logging-1.2.5.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

nrt_logging-1.2.5-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

Details for the file nrt-logging-1.2.5.tar.gz.

File metadata

  • Download URL: nrt-logging-1.2.5.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.4

File hashes

Hashes for nrt-logging-1.2.5.tar.gz
Algorithm Hash digest
SHA256 0eb200690636b0ce0e64283c808cd87f584106c8bd769d1a22217f7ba63dec17
MD5 2d463505d7fcdfc1f561a78d02cd2d2b
BLAKE2b-256 6cd177c24e6bd8a32c03c297351395085968b2c2128efd1d5399d56e101d5f15

See more details on using hashes here.

File details

Details for the file nrt_logging-1.2.5-py3-none-any.whl.

File metadata

  • Download URL: nrt_logging-1.2.5-py3-none-any.whl
  • Upload date:
  • Size: 18.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.4

File hashes

Hashes for nrt_logging-1.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 16b275ff75490c8ba6107d455adfd0b78e77c8121a6e8b1e2db0c861163acb56
MD5 15abfa6640c28c06741e59c8de38cacf
BLAKE2b-256 6f18957f75db8eb47117b3b7034068d95a8b303f2d5ec32289aee79a211706cf

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