标签:
一、基本用法
只需要基本的配置,就可以使用了。
import logging def fun2(): logging.basicConfig(filename="fun2.log",format="%(asctime)s %(message)s",level=logging.DEBUG) logging.debug("this is fun2 log")
二、进行详细配置
首先添加一个fileHandler来配置记录的文件,Formatter来设置记录的格式和时间的格式,getLogger是获得一个指定名字记录器,然后给这个logger添加handler,并设置记录级别,然后可以用相应的级别进行记录了。
import logging def fun1(): logname = "test.log" filehandler = logging.FileHandler(filename=logname,encoding="utf-8") fmter = logging.Formatter(fmt="%(asctime)s %(message)s",datefmt="%Y-%m-%d %H:%M:%S") filehandler.setFormatter(fmter) loger = logging.getLogger(__name__) loger.addHandler(filehandler) loger.setLevel(logging.FATAL) loger.fatal("second log")
其中Formatter配置参数fmt有如下可选参数
%(name)s Name of the logger (logging channel) %(levelno)s Numeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL) %(levelname)s Text logging level for the message ("DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL") %(pathname)s Full pathname of the source file where the logging call was issued (if available) %(filename)s Filename portion of pathname %(module)s Module (name portion of filename) %(lineno)d Source line number where the logging call was issued (if available) %(funcName)s Function name %(created)f Time when the LogRecord was created (time.time() return value) %(asctime)s Textual time when the LogRecord was created %(msecs)d Millisecond portion of the creation time %(relativeCreated)d Time in milliseconds when the LogRecord was created, relative to the time the logging module was loaded (typically at application startup time) %(thread)d Thread ID (if available) %(threadName)s Thread name (if available) %(process)d Process ID (if available) %(message)s The result of record.getMessage(), computed just as the record is emitted
以下皆转自Python logging模块详解
三、额外说明
1 可供选择的日志级别有
CRITICAL = 50 FATAL = CRITICAL ERROR = 40 WARNING = 30 WARN = WARNING INFO = 20 DEBUG = 10 NOTSET = 0
Logger.debug()、Logger.info()、Logger.warning()、Logger.error()、Logger.critical()输出不同级别的日志,只有日志等级大于或等于设置的日志级别的日志才会被输出。
2 Handler
Handler对象负责发送相关的信息到指定目的地,常用方法如下
Handler.setLevel(lel):指定日志级别,低于lel级别的日志将被忽略
Handler.setFormatter():给这个handler选择一个Formatter
Handler.addFilter(filt)、Handler.removeFilter(filt):新增或删除一个filter对象
可以通过addHandler()方法为Logger添加多个Handler
logging.handlers.RotatingFileHandler 类似于上面的FileHandler,但是它可以管理文件大小。当文件达到一定大小之后,它会自动将当前日志文件改名,然后创建一个新的同名日志文件继续输出
logging.handlers.TimedRotatingFileHandler 和RotatingFileHandler类似,不过,它没有通过判断文件大小来决定何时重新创建日志文件,而是间隔一定时间就自动创建新的日志文件
logging.handlers.SocketHandler 使用TCP协议,将日志信息发送到网络。
logging.handlers.DatagramHandler 使用UDP协议,将日志信息发送到网络。
logging.handlers.SysLogHandler 日志输出到syslog
logging.handlers.NTEventLogHandler 远程输出日志到Windows NT/2000/XP的事件日志
logging.handlers.SMTPHandler 远程输出日志到邮件地址
logging.handlers.MemoryHandler 日志输出到内存中的制定buffer
logging.handlers.HTTPHandler 通过"GET"或"POST"远程输出到HTTP服务器
各个Handler的具体用法可查看参考书册:
https://docs.python.org/2/library/logging.handlers.html#module-logging.handlers
标签:
原文地址:http://www.cnblogs.com/zhaopengcheng/p/5801028.html