blob: d0b67caf17444876d7414b031d3bbabd52e8678f [file] [log] [blame]
Wei-Yu Chen49950b92021-11-08 19:19:18 +08001"""
2Copyright 2020 The Magma Authors.
3
4This source code is licensed under the BSD-style license found in the
5LICENSE file in the root directory of this source tree.
6
7Unless required by applicable law or agreed to in writing, software
8distributed under the License is distributed on an "AS IS" BASIS,
9WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10See the License for the specific language governing permissions and
11limitations under the License.
12"""
13
14import logging
15from logging.handlers import RotatingFileHandler
16
17LOG_FILE = './enodebd.log'
18MAX_BYTES = 1024 * 1024 * 10 # 10MB
19BACKUP_COUNT = 5 # 10MB, 5 files, 50MB total
20
21
22class EnodebdLogger:
23 """
24 EnodebdLogger backs up debug logs with a RotatingFileHandler.
25
26 Debug logs will be propagated to root level if the root logger is set to
27 debug level.
28 """
29
30 _LOGGER = logging.getLogger(__name__) # type: logging.Logger
31
32 @staticmethod
33 def init() -> None:
34 if logging.root.level is not logging.DEBUG:
35 EnodebdLogger._LOGGER.propagate = False
36 handler = RotatingFileHandler(
37 LOG_FILE,
38 maxBytes=MAX_BYTES,
39 backupCount=BACKUP_COUNT,
40 )
41 formatter = logging.Formatter(fmt='%(asctime)s %(levelname)s %(message)s')
42 handler.setFormatter(formatter)
43 EnodebdLogger._LOGGER.addHandler(handler)
44 EnodebdLogger._LOGGER.setLevel(logging.DEBUG)
45
46 @staticmethod
47 def debug(msg, *args, **kwargs):
48 EnodebdLogger._LOGGER.debug(msg, *args, **kwargs)
49
50 @staticmethod
51 def info(msg, *args, **kwargs):
52 if not EnodebdLogger._LOGGER.propagate:
53 logging.info(msg, *args, **kwargs)
54 EnodebdLogger._LOGGER.info(msg, *args, **kwargs)
55
56 @staticmethod
57 def warning(msg, *args, **kwargs):
58 if not EnodebdLogger._LOGGER.propagate:
59 logging.warning(msg, *args, **kwargs)
60 EnodebdLogger._LOGGER.warning(msg, *args, **kwargs)
61
62 @staticmethod
63 def error(msg, *args, **kwargs):
64 if not EnodebdLogger._LOGGER.propagate:
65 logging.error(msg, *args, **kwargs)
66 EnodebdLogger._LOGGER.error(msg, *args, **kwargs)
67
68 @staticmethod
69 def exception(msg, *args, **kwargs):
70 if not EnodebdLogger._LOGGER.propagate:
71 logging.exception(msg, *args, **kwargs)
72 EnodebdLogger._LOGGER.exception(msg, *args, **kwargs)
73
74 @staticmethod
75 def critical(msg, *args, **kwargs):
76 if not EnodebdLogger._LOGGER.propagate:
77 logging.critical(msg, *args, **kwargs)
78 EnodebdLogger._LOGGER.critical(msg, *args, **kwargs)