RIC:1060: Change in PTL
[com/pylog.git] / mdclogpy / mdclogpy / __init__.py
1 # Copyright (c) 2019 AT&T Intellectual Property.
2 # Copyright (c) 2018-2019 Nokia.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 # This source code is part of the near-RT RIC (RAN Intelligent Controller)
17 # platform project (RICP).
18 #
19 """Structured logging library with Mapped Diagnostic Context
20
21 Outputs the log entries to standard out in structured format, json currently.
22 Severity based filtering.
23 Supports Mapped Diagnostic Context (MDC).
24
25 Set MDC pairs are automatically added to log entries by the library.
26 """
27
28 from .Logger import Logger
29 from .Logger import Level
30 from .Logger import Value
31
32
33 _root_logger = Logger()
34
35
36 def log(level: Level, message: str):
37     """Log a message."""
38     _root_logger.log(level, message)
39
40
41 def mdclog_format_init(configmap_monitor=False):
42     _root_logger.mdclog_format_init(configmap_monitor)    
43
44
45 def error(message: str):
46     """Log an error message. Equals to log(ERROR, msg)."""
47     _root_logger.log(Level.ERROR, message)
48
49
50 def warning(message: str):
51     """Log a warning message. Equals to log(WARNING, msg)."""
52     _root_logger.log(Level.WARNING, message)
53
54
55 def info(message: str):
56     """Log an info message. Equals to log(INFO, msg)."""
57     _root_logger.log(Level.INFO, message)
58
59
60 def debug(message: str):
61     """Log a debug message. Equals to log(DEBUG, msg)."""
62     _root_logger.log(Level.DEBUG, message)
63
64
65 def set_level(level: Level):
66     """Set current logging level."""
67     _root_logger.set_level(level)
68
69
70 def get_level() -> Level:
71     """Return the current logging level."""
72     return _root_logger.get_level()
73
74
75 def add_mdc(key: str, value: Value):
76     """Add an MDC to the root logger."""
77     _root_logger.add_mdc(key, value)
78
79
80 def get_mdc(key: str) -> Value:
81     """Return root logger's MDC with the given key or None."""
82     return _root_logger.get_mdc(key)
83
84
85 def remove_mdc(key: str):
86     """Remove root logger's MDC with the given key."""
87     _root_logger.remove_mdc(key)
88
89
90 def clean_mdc():
91     """Remove all MDCs from the root logger."""
92     _root_logger.clean_mdc()