Pythonログモジュールの使用方法の概要
紹介する
A logger is configured to have a log level. This log level describes the severity of the messages that the logger will handle. Python defines the following log levels:
Each message that is written to the logger is a Log Record. Each log record also has a log level indicating the severity of that specific message. A log record can also contain useful metadata that describes the event that is being logged. This can include details such as a stack trace or an error code.
When a message is given to the logger, the log level of the message is compared to the log level of the logger. If the log level of the message meets or exceeds the log level of the logger itself, the message will undergo further processing. If it doesn’t, the message will be ignored.
Once a logger has determined that a message needs to be processed, it is passed to a Handler.
(ソース:https://docs.djangoproject.com/en/1.4/topics/logging/)
簡単な例
#-- coding: utf-8 --
import logging
「」「ロゴの簡単な使用例」
"""
ちょっと難しい例
複雑な使い方で、loggerにいくつかのhandlerを追加しました.
また、プロファイルを使用してloggingを構成する方法は、次のとおりです.
プロファイル(log_conf):
Pythonコード:
A logger is configured to have a log level. This log level describes the severity of the messages that the logger will handle. Python defines the following log levels:
§ DEBUG: Low level system information for debugging purposes
§ INFO: General system information
§ WARNING: Information describing a minor problem that has occurred.
§ ERROR: Information describing a major problem that has occurred.
§ CRITICAL: Information describing a critical problem that has occurred.
Each message that is written to the logger is a Log Record. Each log record also has a log level indicating the severity of that specific message. A log record can also contain useful metadata that describes the event that is being logged. This can include details such as a stack trace or an error code.
When a message is given to the logger, the log level of the message is compared to the log level of the logger. If the log level of the message meets or exceeds the log level of the logger itself, the message will undergo further processing. If it doesn’t, the message will be ignored.
Once a logger has determined that a message needs to be processed, it is passed to a Handler.
(ソース:https://docs.djangoproject.com/en/1.4/topics/logging/)
簡単な例
#-- coding: utf-8 --
import logging
「」「ロゴの簡単な使用例」
"""
if __name__ == '__main__':
# , logging
logging.debug(u' ')
logging.info(u' ')
logging.warning(u' , logging WARNING, ')
ちょっと難しい例
#-*- coding: utf-8 -*-
import logging
"""logging
"""
if __name__ == '__main__':
# python , logging logger
FORMAT = '%(asctime)-15s %(clientip)s %(user)-8s %(message)s'
logging.basicConfig(format = FORMAT)
d = { 'clientip' : '192.168.0.1', 'user' : 'fbloggs' }
#
logger = logging.getLogger('tcpserver')
logger.warning('Protocol problem: %s', 'connection reset', extra = d)
#
logger.setLevel(logging.DEBUG)
logger.debug('Hello', extra = d)
複雑な使い方で、loggerにいくつかのhandlerを追加しました.
#-*- coding: utf-8 -*-
import logging, logging.config
"""logging
"""
if __name__ == '__main__':
#
LOGGING = {
# , 1
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'verbose': {'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'},
'simple': {'format': '%(levelname)s %(message)s'},
'default': {
'format' : '%(asctime)s %(message)s',
'datefmt' : '%Y-%m-%d %H:%M:%S'
}
},
'handlers': {
'null': {
'level':'DEBUG',
'class':'logging.NullHandler',
},
'console':{
'level':'DEBUG',
'class':'logging.StreamHandler',
'formatter': 'default'
},
'file': {
'level': 'INFO',
# TimedRotatingFileHandler ,
# ' + '
# Python handler, logging.handlers
'class': 'logging.handlers.TimedRotatingFileHandler',
'formatter': 'default',
# TimedRotatingFileHandler
#
# filename
'filename' : 'log',
# 5
'when' : 'M',
'interval' : 1,
'encoding' : 'utf8',
}
},
'loggers' : {
# logger
'mylogger' : {
'level' : 'DEBUG',
'handlers' : ['console', 'file'],
'propagate' : True
}
}
}
logging.config.dictConfig(LOGGING)
logger = logging.getLogger('mylogger')
logger.info('Hello')
また、プロファイルを使用してloggingを構成する方法は、次のとおりです.
プロファイル(log_conf):
[loggers]
keys=root,mylogger
[handlers]
keys=null,console,file
[formatters]
keys=verbose,simple,default
[formatter_verbose]
format=%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s
datefmt=
class=logging.Formatter
[formatter_simple]
format=%(levelname)s %(message)s
datefmt=
class=logging.Formatter
[formatter_default]
format=%(asctime)s %(message)s
datefmt=%Y-%m-%d %H:%M:%S
class=logging.Formatter
[logger_mylogger]
level=DEBUG
handlers=console,file
propagate=1
qualname=
[logger_root]
level=NOTSET
handlers=
[handler_null]
class=NullHandler
level=DEBUG
args=()
[handler_console]
class=StreamHandler
level=DEBUG
args=()
[handler_file]
class=handlers.TimedRotatingFileHandler
level=INFO
formatter=default
args=('log','M',1,0,'utf8')
Pythonコード:
#-*- coding: utf-8 -*-
import logging, logging.config
"""logging
"""
if __name__ == '__main__':
#
logging.config.fileConfig('log_conf')
logger = logging.getLogger('mylogger')
logger.info('Hello')