python loggingログモジュールの詳細

5299 ワード

python loggingログモジュールの詳細
ログ・レベル

      5   ,       :DEBUG INFO WARNING ERROR CRITICAL。
DEBUG:     ,           
INFO:         
WARNING:      ,            ,             (  。     ”)。           。
ERROR:      ,          
CRITICAL:       ,               
 5   ,     5       : debug 、info 、warning 、error 、critical。    WARNING,  WARNING        。

ログ形式の説明

logging.basicConfig   ,           format,               ,     :
%(levelno)s:          
%(levelname)s:         
%(pathname)s:            ,    sys.argv[0]
%(filename)s:          
%(funcName)s:          
%(lineno)d:          
%(asctime)s:        
%(thread)d:     ID
%(threadName)s:       
%(process)d:     ID
%(message)s:       
                   。  :
format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s'
               ,        ,          ,         。

ログ出力
トレースを記録するには、出力コンソールとログファイルなどのファイルに記録する方法が2つあります.
コンソールへのログの出力
a.pyに次の情報を書き込む

import logging 

logging.basicConfig(level=logging.WARNING, 
          format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s') 
# use logging 
logging.info('this is a loggging info message') 
logging.debug('this is a loggging debug message') 
logging.warning('this is loggging a warning message') 
logging.error('this is an loggging error message') 
logging.critical('this is a loggging critical message') 
         Console       :
2017-03-16 16:58:11,266 - a.py[line:10] - WARNING: this is loggging a warning message
2017-03-16 16:58:11,266 - a.py[line:11] - ERROR: this is an loggging error message
2017-03-16 16:58:11,266 - a.py[line:12] - CRITICAL: this is a loggging critical message


【解析】
loggingでbasicConfig関数はログの出力フォーマットと方式に関連して配置し、上のコードはログの出力レベルがWARNINGレベルであることを設定し、WARNINGレベル以上のログが出力されることを意味する.また,ログ出力のフォーマットも作成した.
ファイルへのログの出力
logging.basicConfig関数では、出力ファイルのファイル名と書き込みモードを設定します.

import logging 

logging.basicConfig(level=logging.WARNING, 
          filename='./log/log.txt', 
          filemode='w', 
          format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s') 
# use logging 
logging.info('this is a loggging info message') 
logging.debug('this is a loggging debug message') 
logging.warning('this is loggging a warning message') 
logging.error('this is an loggging error message') 
logging.critical('this is a loggging critical message') 
    ,     ./log/log.txt,    :
2015-05-21 17:30:20,282 - log.py[line:12] - WARNING: this is loggging a warning message
2015-05-21 17:30:20,282 - log.py[line:13] - ERROR: this is an loggging error message
2015-05-21 17:30:20,282 - log.py[line:14] - CRITICAL: this is a loggging critical message


プロファイルによるログ・モードの設定
https://docs.python.org/2/library/logging.config.html#logging.config.dictConfig
dictconfigはfileconfigより更新されます

#config.conf
###############################################
[loggers]
keys=root,example01,example02

[logger_root]
level=DEBUG
handlers=hand01,hand02

[logger_example01]
handlers=hand01,hand02
qualname=example01
propagate=0

[logger_example02]
handlers=hand01,hand03
qualname=example02
propagate=0

###############################################
[handlers]
keys=hand01,hand02,hand03

[handler_hand01]
class=StreamHandler
level=INFO
formatter=form02
args=(sys.stderr,)

[handler_hand02]
class=FileHandler
level=NOTSET
formatter=form01
args=('myapp.log', 'a')

[handler_hand03]
class=handlers.RotatingFileHandler
level=INFO
formatter=form02
args=('myapp.log', 'a', 10*1024*1024, 5)

###############################################
[formatters]
keys=form01,form02
[formatter_form01]
format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s
datefmt=%a, %d %b %Y %H:%M:%S
[formatter_form02]
format=%(name)-12s: %(levelname)-8s %(message)s
datefmt=


しゅかんすう

import logging
import logging.config

logging.config.fileConfig("/home/razerware/configscript/config.conf")
logger = logging.getLogger("example01")
logger2 = logging.getLogger("example02")
logger.debug('This is debug message')
logger.info('This is info message')
logger.warning('This is warning message')

logger2.debug('This is debug message')
logger2.info('This is info message')
logger2.warning('This is warning message')


もし疑問があれば伝言を残してあるいは当駅のコミュニティに行って討論を交流して、読書に感謝して、みんなを助けることができることを望んで、みんなの当駅に対する支持に感謝します!