Skip to content

Python logger

python
from loguru import logger


class LoggerHelper:

    def __init__(self, name="file.log"):
        logger.add(name, filter=self._ignore_info, rotation="500 MB")

    def _ignore_info(self, record):
        return record["level"].name != "INFO"

    def info(self, msg):
        logger.info(msg)

    def debug(self, msg):
        logger.debug(msg)

    def exc(self, msg):
        logger.exception(msg)


def test():
    xlogger = LoggerHelper("file.log")
    xlogger.info("info msg")
    xlogger.debug("debug msg")
    try:
        1 / 0
    except ZeroDivisionError as e:
        xlogger.exc("An error occurred")

if __name__ == '__main__':
    test()

Released under the MIT License.