Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 1 | """ |
| 2 | Copyright 2020 The Magma Authors. |
| 3 | |
| 4 | This source code is licensed under the BSD-style license found in the |
| 5 | LICENSE file in the root directory of this source tree. |
| 6 | |
| 7 | Unless required by applicable law or agreed to in writing, software |
| 8 | distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | See the License for the specific language governing permissions and |
| 11 | limitations under the License. |
| 12 | """ |
| 13 | |
| 14 | import logging |
| 15 | from logging.handlers import RotatingFileHandler |
| 16 | |
| 17 | LOG_FILE = './enodebd.log' |
| 18 | MAX_BYTES = 1024 * 1024 * 10 # 10MB |
| 19 | BACKUP_COUNT = 5 # 10MB, 5 files, 50MB total |
| 20 | |
| 21 | |
| 22 | class 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) |