| """ |
| Copyright 2020 The Magma Authors. |
| |
| This source code is licensed under the BSD-style license found in the |
| LICENSE file in the root directory of this source tree. |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| """ |
| |
| import logging |
| from logging.handlers import RotatingFileHandler |
| |
| LOG_FILE = './enodebd.log' |
| MAX_BYTES = 1024 * 1024 * 10 # 10MB |
| BACKUP_COUNT = 5 # 10MB, 5 files, 50MB total |
| |
| |
| class EnodebdLogger: |
| """ |
| EnodebdLogger backs up debug logs with a RotatingFileHandler. |
| |
| Debug logs will be propagated to root level if the root logger is set to |
| debug level. |
| """ |
| |
| _LOGGER = logging.getLogger(__name__) # type: logging.Logger |
| |
| @staticmethod |
| def init() -> None: |
| if logging.root.level is not logging.DEBUG: |
| EnodebdLogger._LOGGER.propagate = False |
| handler = RotatingFileHandler( |
| LOG_FILE, |
| maxBytes=MAX_BYTES, |
| backupCount=BACKUP_COUNT, |
| ) |
| formatter = logging.Formatter(fmt='%(asctime)s %(levelname)s %(message)s') |
| handler.setFormatter(formatter) |
| EnodebdLogger._LOGGER.addHandler(handler) |
| EnodebdLogger._LOGGER.setLevel(logging.DEBUG) |
| |
| @staticmethod |
| def debug(msg, *args, **kwargs): |
| EnodebdLogger._LOGGER.debug(msg, *args, **kwargs) |
| |
| @staticmethod |
| def info(msg, *args, **kwargs): |
| if not EnodebdLogger._LOGGER.propagate: |
| logging.info(msg, *args, **kwargs) |
| EnodebdLogger._LOGGER.info(msg, *args, **kwargs) |
| |
| @staticmethod |
| def warning(msg, *args, **kwargs): |
| if not EnodebdLogger._LOGGER.propagate: |
| logging.warning(msg, *args, **kwargs) |
| EnodebdLogger._LOGGER.warning(msg, *args, **kwargs) |
| |
| @staticmethod |
| def error(msg, *args, **kwargs): |
| if not EnodebdLogger._LOGGER.propagate: |
| logging.error(msg, *args, **kwargs) |
| EnodebdLogger._LOGGER.error(msg, *args, **kwargs) |
| |
| @staticmethod |
| def exception(msg, *args, **kwargs): |
| if not EnodebdLogger._LOGGER.propagate: |
| logging.exception(msg, *args, **kwargs) |
| EnodebdLogger._LOGGER.exception(msg, *args, **kwargs) |
| |
| @staticmethod |
| def critical(msg, *args, **kwargs): |
| if not EnodebdLogger._LOGGER.propagate: |
| logging.critical(msg, *args, **kwargs) |
| EnodebdLogger._LOGGER.critical(msg, *args, **kwargs) |