Skip to main content

Hierarchical logging in yaml format

Project description

#Hierarchical logging in yaml format.

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

Logging fields can be in Yaml format or in Line format.

In addition to automatic hierarchy, manually hierarchy is supported.

###Example:

Example of

 - A.a1() - WARN
   - B.child_1() - INFO
     - B.child_2() - INFO
from nrt_logging.logger import logger_manager, NrtLogger
from nrt_logging.logger_stream_handlers import \
    ConsoleStreamHandler, LogStyleEnum

NAME_1 = 'name 1'


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 A:
    __logger: NrtLogger
    __child: Child

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

    def a1(self):
        self.__logger.warn('Message 1')
        self.__child.child_1()

Init logger with LINE style

def logging_line_style():
    sh = ConsoleStreamHandler()
    sh.log_style = LogStyleEnum.LINE
    logger = logger_manager.get_logger(NAME_1)
    logger.add_stream_handler(sh)
    a = A()
    a.a1()
 
logging_line_style()

Output

  • log: 2022-10-13 00:22:57.676415 [WARN] [test_line_style.py.A.a1:29] Message 1 children:
    • log: 2022-10-13 00:22:57.688419 [INFO] [test_line_style.py.Child.child_1:13] Child 1 children:
      • log: 2022-10-13 00:22:57.700451 [INFO] [test_line_style.py.Child.child_2:17] Child 2

Init logger with YAML style

def logging_yaml_style():
    sh = ConsoleStreamHandler()
    sh.log_style = LogStyleEnum.YAML
    logger = logger_manager.get_logger(NAME_1)
    logger.add_stream_handler(sh)
    a = A()
    a.a1()

logging_yaml_style()

Output

---
date: 2022-10-13 00:29:52.548451
log_level: WARN
path: test_line_style.py.A
method: a1
line_number: 31
message: Message 1
children:
  - date: 2022-10-13 00:29:52.558965
    log_level: INFO
    path: test_line_style.py.Child
    method: child_1
    line_number: 15
    message: Child 1
    children:
      - date: 2022-10-13 00:29:52.569975
        log_level: INFO
        path: test_line_style.py.Child
        method: child_2
        line_number: 19
        message: Child 2

Example of changing hierarchy manually

from nrt_logging.log_level import LogLevelEnum
from nrt_logging.logger import logger_manager
from nrt_logging.logger_stream_handlers import ConsoleStreamHandler, LogStyleEnum


sh = ConsoleStreamHandler()
sh.log_level = LogLevelEnum.TRACE
sh.log_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-13 00:35:43.310527 [INFO] [test_manual_hierarchy_logging.py.<module>:12] main level log
  children:
    - log: 2022-10-13 00:35:43.318527 [INFO] [test_manual_hierarchy_logging.py.<module>:14] child 1
      children:
        - log: 2022-10-13 00:35:43.325527 [INFO] [test_manual_hierarchy_logging.py.<module>:16] child 1_1
    - log: 2022-10-13 00:35:43.333531 [INFO] [test_manual_hierarchy_logging.py.<module>:18] child 2
- log: 2022-10-13 00:35:43.341532 [INFO] [test_manual_hierarchy_logging.py.<module>:20] continue main level

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

nrt_logging-1.0.1-py3-none-any.whl (21.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nrt_logging-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 21.3 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.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 961ac2e4b8fe42b724f6cba4d9f9ef487f9412a3a83c55ce7b1cdafa448b67e5
MD5 b99a19aa308ed8a4d1d36c58c724507c
BLAKE2b-256 778497564b8352819afc06126dfdd1e1c3c6ae3d3ce7e33665b5642c443ab9ba

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