0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ロガーっていいなぁ

Posted at

こんな書き方もできるし、

import logging


class LoggerBase(logging.Logger):
    _instance = None

    def __new__(cls) -> logging.Logger:
        if cls._instance is None:
            logging.basicConfig(
                format="%(asctime)s\t%(levelname)s\t%(message)s",
                level=logging.INFO,
            )
            cls._instance = logging.getLogger(__name__)
            return cls._instance


class Logger(LoggerBase):
    _logger = LoggerBase()

    @classmethod
    def info(cls, message: str):
        return cls._logger.info(msg=message)

    @classmethod
    def warning(cls, message: str):
        return cls._logger.warning(msg=message)

    @classmethod
    def error(cls, message: str):
        return cls._logger.error(msg=message, exc_info=True)


if __name__ == "__main__":
    Logger.info("テスト")

こっちのほうがスッキリしている気がする。

import logging


class Logger:
    _logger = None

    @classmethod
    def set_logger(cls, name: str):
        logging.basicConfig(
            format="%(name)s\t%(asctime)s\t%(levelname)s\t%(message)s",
            level=logging.INFO,
        )
        cls._logger = logging.getLogger(name)
        return cls._logger

    @classmethod
    def info(cls, msg: str):
        return cls._logger.info(msg=msg)

    @classmethod
    def warning(cls, msg: str):
        return cls._logger.warning(msg=msg)

    @classmethod
    def error(cls, msg: str):
        return cls._logger.error(msg=msg, exc_info=True)


if __name__ == "__main__":
    Logger.set_logger("pythonic")
    Logger.info("テステス")

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?