Help us understand the problem. What is going on with this article?

pythonのlog出力

More than 1 year has passed since last update.

はじめに

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

最後に

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした