博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python模块之logging
阅读量:4677 次
发布时间:2019-06-09

本文共 3412 字,大约阅读时间需要 11 分钟。

在Python HOWTO里面介绍了什么时候去用这个模块。我觉得就是你想用的时候就行(废话嘛。。。。。)(例子来源于Python HOWTO)

1、basicConfig

logging.basicConfig(filename='example.log',level=logging.DEBUG)logging.debug('This message should go to the log file')logging.info('So should this')logging.warning('And this, too')

basicConfig里面的参数有filename,filemode,level,format等

filename:你的日志存入的文件名称

filenmode:你的日志文件的打开方式,同文件打开的方式一样

level:你的日志等级

  其中等级分为如下几个:从上到下依次升高

  NOSET:不设置任何,即任何等级都可以记录

  DEBUG:

  INFO:

  WARNING:

  ERROR:

  CRITICAL:

  例子:

  

import logginglogging.basicConfig(filename='example.log',level=logging.INFO)logging.debug('This message should go to the log file')logging.info('So should this')logging.warning('And this, too')

  输出就不会出现debug,因为debug的等级比info的小,除此之外,default为warning

format:用来设定日志输出的方式

e.g.

import logginglogging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG) # 里面格式化参数地址为http://docs.python.org/2/library/logging.html#logre                                                                             # cord-attributes,其中也可以设置时间csctimelogging.debug('This message should appear on the console')logging.info('So should this')logging.warning('And this, too')

输出的文件内容为

DEBUG:This message should appear on the consoleINFO:So should thisWARNING:And this, too

2、上面只是一些基本用法,还有一些高级一点儿的用法logger,handler,formatter

  logger:日志对象logger.setLevel() logger.addHandler() logger.removeHandler() logger.debug() logger.info logger.warning logger. error() logger.addFilter()

      logger.removeFilter()

  handler:关于日志的处理的一些参数handler.setLevel() handler.setFormatter() handler.addFilter() handler.removeFilter()

  formatter:设定日志输出的形式,常见形式:'%(asctime)s - %(levelname)s - %(message)s'

  

import logging# create loggerlogger = logging.getLogger('simple_example')logger.setLevel(logging.DEBUG)# create console handler and set level to debugch = logging.StreamHandler() # 如果写入文件可改成FileHandlerch.setLevel(logging.DEBUG)# create formatterformatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')# add formatter to chch.setFormatter(formatter)# add ch to loggerlogger.addHandler(ch)# 'application' codelogger.debug('debug message')logger.info('info message')logger.warn('warn message')logger.error('error message')logger.critical('critical message')

3、还可以从配置文件中读取log的参数

下面是配置文件loggin.conf

[loggers]keys=root,simpleExample[handlers]keys=consoleHandler[formatters]keys=simpleFormatter[logger_root]level=DEBUGhandlers=consoleHandler[logger_simpleExample]level=DEBUGhandlers=consoleHandlerqualname=simpleExamplepropagate=0[handler_consoleHandler]class=StreamHandlerlevel=DEBUGformatter=simpleFormatterargs=(sys.stdout,)[formatter_simpleFormatter]format=%(asctime)s - %(name)s - %(levelname)s - %(message)sdatefmt=

程序为:

import loggingimport logging.configlogging.config.fileConfig('logging.conf')# create loggerlogger = logging.getLogger('simpleExample')# 'application' codelogger.debug('debug message')logger.info('info message')logger.warn('warn message')logger.error('error message')logger.critical('critical message')

当然还可以,另一种格式叫做字典的格式

version: 1formatters:  simple:    format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'handlers:  console:    class: logging.StreamHandler    level: DEBUG    formatter: simple    stream: ext://sys.stdoutloggers:  simpleExample:    level: DEBUG    handlers: [console]    propagate: noroot:  level: DEBUG  handlers: [console]

 

 

转载于:https://www.cnblogs.com/MyselfDancing/p/3574312.html

你可能感兴趣的文章
经典,程序员笑话
查看>>
Linux 抓取网站命令
查看>>
浪叫兽的自我介绍 (完整版) 讲述一段如何进入大数据行业
查看>>
Qt ui在程序中的使用
查看>>
datatables.js 简单使用--弹出编辑框或添加数据框
查看>>
前端--3、jQuery
查看>>
最小化的 Google Analytics 代码
查看>>
服务器监控相关
查看>>
转: 环信联合创始人:App主流反垃圾服务难点和技术实现全解析
查看>>
关于类型转换这件事
查看>>
[转]30分钟,让你成为一个更好的程序员
查看>>
《使用Hibernate开发租房系统》内部测试笔试题
查看>>
矩阵的奇异值与特征值
查看>>
窗体切换中的小技巧
查看>>
day10作业
查看>>
2013-5-11 湘潭多省程序设计 赛后总结
查看>>
SEO页面优化
查看>>
读构建之法第一天
查看>>
Redis(二)、Redis持久化RDB和AOF
查看>>
浅谈OC内存管理
查看>>