1 # ONAP python logging package
2 - python-package onappylog extend python standard logging library which
3 could be used in any python project to log MDC(Mapped Diagnostic Contex)
4 and easy to reload logging at runtime.
15 ### 1. MDC monkey patch
17 Import the MDC monkey patch making logRecord to store context in local thread.
20 from onaplogging import monkey; monkey.patch_loggingMDC()
22 Import the MDC format to be used to configure mdc output format.
23 Please replace your old logging format with mdc format in configuration.
27 from onaplogging import mdcformatter
29 the mdc format example
32 '()': mdcformatter.MDCFormatter, # Use MDCFormatter instance to convert logging string
33 'format': '%(mdc)s and other %-style key ', # Add '%(mdc)s' here.
34 'mdcfmt': '{key1} {key2}', # Define your mdc keys here.
35 'datefmt': '%Y-%m-%d %H:%M:%S' # date format
39 Import MDC to store context in python file with logger
43 from onaplogging.mdcContext import MDC
45 MDC.put("key1", "value1")
46 MDC.put("key2", "value2")
54 ### 2. Reload logging at runtime
56 It's thread safe to reload logging. If you want to use this feature,
57 must use yaml file to configure logging.
60 import the yaml monkey patch and load logging yaml file
63 from onaplogging import monkey,monkey.patch_loggingYaml()
65 config.yamlConfig(filepath=<yaml filepath>, watchDog=True)
68 Notice that the watchDog is opening,So your logging could be reloaded at runtime.
69 if you modify yaml file to change handler、filter or format,
70 the logger in program will be reloaded to use new configuration.
72 Set watchDog to **false**, If you don't need to reloaded logging.
77 Yaml configure exmaple
82 disable_existing_loggers: True
87 handlers: [vioHandler]
91 class: logging.handlers.RotatingFileHandler
93 filename: /var/log/bt.log
95 maxBytes: 1024*1024*50
97 formatter: mdcFormatter
100 format: "%(asctime)s:[%(name)s] %(created)f %(module)s %(funcName)s %(pathname)s %(process)d %(levelno)s :[ %(threadName)s %(thread)d]: [%(mdc)s]: [%(filename)s]-[%(lineno)d] [%(levelname)s]:%(message)s"
101 mdcfmt: "{key1} {key2} {key3} {key4} dwdawdwa "
102 datefmt: "%Y-%m-%d %H:%M:%S"
103 (): onaplogging.mdcformatter.MDCFormatter
105 format: '%(asctime)s:[%(name)s]:[%(filename)s]-[%(lineno)d]
106 [%(levelname)s]:%(message)s '
107 datefmt: "%Y-%m-%d %H:%M:%S"
114 [What's MDC?](https://logging.apache.org/log4j/2.x/manual/thread-context.html)
116 [Onap Logging Guidelines](https://wiki.onap.org/pages/viewpage.action?pageId=20087036)
118 [Python Standard Logging Library](https://docs.python.org/2/library/logging.html)