Introduced
A logger is configured to has a log level. This log level describes the severity of the messages, the logger would 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 of the message is written to the logger is a Log Record. Each log record also have a log level indicating the severity of this specific message. A log record can also contain useful metadata that describes the event it is being logged. This can include details such as a stack trace or an error code.
When a message was given to the logger, the log level of the message was 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 would undergo further proc Essing. If it doesn ' t, the message would be ignored.
Once a logger have determined that a message needs to be processed, it's passed to a Handler.
(Source: https://docs.djangoproject.com/en/1.4/topics/logging/)
A simple example
#--Coding:utf-8--
Import logging
Simple use example of "" "Logging
"""
if __name__ == ‘__main__‘:# 最简单的用法,不推荐直接用logging模块logging.debug(u‘这个不会被打印出来‘)logging.info(u‘这个也不会被打印出来‘)logging.warning(u‘这个就会了,因为logging默认的级别是WARNING,低于这个等级的信息就会被忽略‘)
A slightly harder example
#-*- 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)
A more complex usage, adding several handler to the logger.
#-*-coding:utf-8-*-import logging, logging.config "" "Logging use Example" "If __name__ = = ' __main__ ': # more complex usage L ogging = {# version, always 1 ' version ': 1, ' dis Able_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 writes the log to the file at a certain time interval and # Rename the file to ' original filename + time timestamp ' form # python provides additional handler, reference L Ogging.handlers ' class ': ' Logging.handlers.TimedRo Tatingfilehandler ', ' Formatter ': ' Default ', # These will be passed to Timedrotatingfilehandler in the form of arguments. # constructors # filename in the directory to ensure the existence of ' filename ': ' L og ', # refresh every 5 minutes ' When ': ' M ', ' interval ': 1, ' Encoding ': ' UTF8 ', }}, ' loggers ': { # defines a logger ' MyLogger ': { ' Level ': ' DEBUG ', ' Handlers ': [' console ', ' file '], ' Propagate ': True }}} logging.config.dictConfig (logging) logger = Logging.getlogge R (' MyLogger ') logGer.info (' Hello ')
In addition, you can configure logging through the configuration file as follows:
Configuration file (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)sdatefmt=class=logging.Formatter[formatter_simple]format=%(levelname)s %(message)sdatefmt=class=logging.Formatter[formatter_default]format=%(asctime)s %(message)sdatefmt=%Y-%m-%d %H:%M:%Sclass=logging.Formatter[logger_mylogger]level=DEBUGhandlers=console,filepropagate=1qualname=[logger_root]level=NOTSEThandlers=[handler_null]class=NullHandlerlevel=DEBUGargs=()[handler_console]class=StreamHandlerlevel=DEBUGargs=()[handler_file]class=handlers.TimedRotatingFileHandlerlevel=INFOformatter=defaultargs=(‘log‘,‘M‘,1,0,‘utf8‘)
Python code:
#-*- coding: utf-8 -*-import logging, logging.config"""logging的使用示例"""if __name__ == ‘__main__‘: # 使用配置文件 logging.config.fileConfig(‘log_conf‘) logger = logging.getLogger(‘mylogger‘) logger.info(‘Hello‘)
How to use the Python log (logging) module