最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

python通过logging写入日志到文件和控制台的实例

来源:动视网 责编:小OO 时间:2020-11-27 14:21:51
文档

python通过logging写入日志到文件和控制台的实例

如下所示。,import logging # 创建一个logger logger = logging.getLogger(';mylogger';) logger.setLevel(logging.DEBUG) # 创建一个handler,用于写入日志文件 fh = logging.FileHandler(';test.log';) fh.setLevel(logging.DEBUG) # 再创建一个handler。
推荐度:
导读如下所示。,import logging # 创建一个logger logger = logging.getLogger(';mylogger';) logger.setLevel(logging.DEBUG) # 创建一个handler,用于写入日志文件 fh = logging.FileHandler(';test.log';) fh.setLevel(logging.DEBUG) # 再创建一个handler。


下面为大家分享一篇python 通过logging写入日志到文件和控制台的实例,具有很好的参考价值,希望对大家有所帮助。一起过来看看吧

如下所示:

import logging 

# 创建一个logger 
logger = logging.getLogger('mylogger') 
logger.setLevel(logging.DEBUG) 
# 创建一个handler,用于写入日志文件 
fh = logging.FileHandler('test.log') 
fh.setLevel(logging.DEBUG) 
# 再创建一个handler,用于
输出到控制台 ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) # 定义handler的输出格式 formatter = logging.Formatter('[%(asctime)s][%(thread)d][%(filename)s][line: %(lineno)d][%(levelname)s] ## %(message)s') fh.setFormatter(formatter) ch.setFormatter(formatter) # 给logger添加handler logger.addHandler(fh) logger.addHandler(ch) # 记录一条日志 logger.info('foorbar')

关于formatter的配置,采用的是%(<dict key>)s的形式,就是字典的关键字替换。提供的关键字包括:

FormatDescription
%(name)sName of the logger (logging channel).
%(levelno)sNumeric logging level for the message (DEBUG, INFO, WARNING, ERROR, CRITICAL).
%(levelname)sText logging level for the message ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL').
%(pathname)sFull pathname of the source file where the logging call was issued (if available).
%(filename)sFilename portion of pathname.
%(module)sModule (name portion of filename).
%(funcName)sName of function containing the logging call.
%(lineno)dSource line number where the logging call was issued (if available).
%(created)fTime when the LogRecord was created (as returned by time.time()).
%(relativeCreated)dTime in milliseconds when the LogRecord was created, relative to the time the logging module was loaded.
%(asctime)sHuman-readable time when the LogRecord was created. By default this is of the form “2003-07-08 16:49:45,896” (the numbers after the comma are millisecond portion of the time).
%(msecs)dMillisecond portion of the time when the LogRecord was created.
%(thread)dThread ID (if available).
%(threadName)sThread name (if available).
%(process)dProcess ID (if available).
%(message)sThe logged message, computed as msg % args.

文档

python通过logging写入日志到文件和控制台的实例

如下所示。,import logging # 创建一个logger logger = logging.getLogger(';mylogger';) logger.setLevel(logging.DEBUG) # 创建一个handler,用于写入日志文件 fh = logging.FileHandler(';test.log';) fh.setLevel(logging.DEBUG) # 再创建一个handler。
推荐度:
标签: 日志 python 控制台
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top