85
65

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

pythonのlog出力

Last updated at Posted at 2018-02-14

はじめに

pythonのlog出力について検索すると、「コレはするな」「アレはダメだ」と講釈を垂れている記事が散見され、なにが正しいのか分からず混乱し、「そんなこと言われてもどうすりゃいいのさ (AA略」となってしまった方が数多くいると思う。
かく言う私もその一人でして。
私のようなコピペプログラマからすると「ゴチャゴチャ文句言うくらいならコピペで使えるコードを寄越せ!」と言いたくなる。

そんな私が適当に作ったloggerを公開してみようと思う。
本音:現場変わったときにコピペして使いまわそう

断っておくが私は細かいことはサッパリ理解していない
しかし、これで不便を感じた事もない。

logger

logger.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from logging import Formatter, handlers, StreamHandler, getLogger, DEBUG


class Logger:
    def __init__(self, name=__name__):
        self.logger = getLogger(name)
        self.logger.setLevel(DEBUG)
        formatter = Formatter("[%(asctime)s] [%(process)d] [%(name)s] [%(levelname)s] %(message)s")

        # stdout
        handler = StreamHandler()
        handler.setLevel(DEBUG)
        handler.setFormatter(formatter)
        self.logger.addHandler(handler)

        # file
        handler = handlers.RotatingFileHandler(filename = 'your_log_path.log',
                                               maxBytes = 1048576,
                                               backupCount = 3)
        handler.setLevel(DEBUG)
        handler.setFormatter(formatter)
        self.logger.addHandler(handler)

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

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

    def warn(self, msg):
        self.logger.warning(msg)

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

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

標準出力(stdout)

>>> import logger
>>> log = logger.Logger('hoge')
>>>
>>> log.debug('test debug')
[2018-02-14 09:54:58,290] [6705] [hoge] [DEBUG] test debug
>>>
>>> log.info('test info')
[2018-02-14 09:55:19,130] [6705] [hoge] [INFO] test info
>>>
>>> log.error('test error')
[2018-02-14 09:55:31,340] [6705] [hoge] [ERROR] test error
>>>
>>> log.warn('test warn')
[2018-02-14 09:55:42,635] [6705] [hoge] [WARNING] test warn
>>>
>>> log.critical('test critical')
[2018-02-14 09:55:57,845] [6705] [hoge] [CRITICAL] test critical
>>>

ファイル出力(fileout)

xxx@xxxxx:~$ cat log.log
[2018-02-14 09:54:58,290] [6705] [hoge] [DEBUG] test debug
[2018-02-14 09:55:19,130] [6705] [hoge] [INFO] test info
[2018-02-14 09:55:31,340] [6705] [hoge] [ERROR] test error
[2018-02-14 09:55:42,635] [6705] [hoge] [WARNING] test warn
[2018-02-14 09:55:57,845] [6705] [hoge] [CRITICAL] test critical

最後に

私は細かいことはサッパリ理解していない。(重要)
ツッコミ、改善、大歓迎です。

85
65
1

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
85
65

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?