日志处理之logging模块
日志级别:
'CRITICAL': CRITICAL,
'ERROR': ERROR,
'WARN': WARNING,
'WARNING': WARNING,
'INFO': INFO,
'DEBUG': DEBUG,
'NOTSET': NOTSET,
import logging
logging.warning("user [wy] attempted wrong password more than 3 times")
logging.critical("server is down")
输出:
WARNING:root:user [wy] attempted wrong password more than 3 times
CRITICAL:root:server is down 如何把日志写入到文件中?
import logging
logging.basicConfig(filename="example.log",level=logging.INFO) #filename后面是日志文件名,level为INFO级别,日志文件会记录INFO级别以上的日志
logging.warning("user [wy] attempted wrong password more than 3 times")
logging.info("show this")
logging.debug("this is erro")
logging.critical("server is down")
生成一个新的文件example.log
example.log记录:
WARNING:root:user [wy] attempted wrong password more than 3 times
INFO:root:show this
CRITICAL:root:server is down 日志级别:debug<info<warning<critical
怎么在日志中加入时间?
import logging
logging.basicConfig(filename="example.log",level=logging.INFO,format="%(asctime)s %(message)s",datefmt="%m/%d/%Y %I:%M:%S %p") #注意大小写
logging.warning("user [wy] attempted wrong password more than 3 times")
logging.info("show this")
logging.debug("this is erro")
logging.critical("server is down") example.log显示:
10/20/2016 08:49:32 PM user [wy] attempted wrong password more than 3 times
10/20/2016 08:49:32 PM show this
10/20/2016 08:49:32 PM server is down
实际生产中,我们要把日志打印到文件中,也要打印到屏幕上。
在这里我们需要了解日志模块的一些复杂的知识了,这里有4个功能:Loggers,Handlers,Filters,Formatters
1 Loggers expose the interface that application code directly #直接调用
2 Handlers send the log records (created by loggers) to the appropriate destination #把日志发送到不同的地方(屏幕,本地文件等)
3 Filters provide a finer grained facility for determing which log records to output #日志过滤,
4 Formatters specify the layout of log records in the final output #字符串格式化
要求:我们要把INFO级别以上的日志打印到屏幕上,把warning级别以上的日志打印到文件中。
import logging
logger = logging.getLogger("TEST-LOG") #获取日志对象
logger.setLevel(logging.DEBUGE) #设置全局日志级别 ch = logging.StreamHandler() #把日志打印到屏幕
ch.setLevel(logging.INFO) #打印到屏幕的日志级别 fh = logging.FileHandler("access.log") #输出到access.log文件中
fh.setLevel(logging.WARNING) #输出到文件中的日志级别 formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") #格式化输出日志
ch.setFormatter(formatter) #打印到屏幕上的日志格式
fh.setFormatter(formatter)#打印到文件中的日志 logger.addHandler(ch) #告诉logger输出日志到具体的handler
logger.addHandler(fh)#告诉logger输出日志到具体的handler 我们可以给打印到屏幕上的日志和打印到文件中的日志定义不同的格式 logger.debug("debug message")
logger.info("info message")
logger.error("error message")
logger.warning("warning message")
logger.critical("critical message") 注意:当全局日志级别高于我们自定义到日志文件中和屏幕上的日志级别时,会按照全局日志级别来打印日志到文件中和屏幕上
当全局日志级别低于我们自定义到日志文件中和屏幕上的日志级别时,会按照我们自定义的日志级别来打印
日志处理之logging模块的更多相关文章
- Python之日志处理(logging模块)
本节内容 日志相关概念 logging模块简介 使用logging提供的模块级别的函数记录日志 logging模块日志流处理流程 使用logging四大组件记录日志 配置logging的几种方式 向日 ...
- 【转】Python之日志处理(logging模块)
[转]Python之日志处理(logging模块) 本节内容 日志相关概念 logging模块简介 使用logging提供的模块级别的函数记录日志 logging模块日志流处理流程 使用logging ...
- Python之日志处理(logging模块)《转载》
Python之日志处理(logging模块): https://www.cnblogs.com/yyds/p/6901864.html
- Python之日志处理(logging模块)转载
本人主要做一个知识的归类与记录,如是转载类文章,居首都会备注原链接,尊重原创者,谢谢! 此文转载原链接:https://www.cnblogs.com/yyds/p/6901864.html 本节内容 ...
- Python之日志处理(logging模块一基础)
转载自:https://www.cnblogs.com/yyds/p/6901864.html 本节内容 日志相关概念 logging模块简介 使用logging提供的模块级别的函数记录日志 logg ...
- 以打印日志为荣之logging模块详细使用
啄木鸟社区里的Pythonic八荣八耻有一条: 以打印日志为荣 , 以单步跟踪为耻; 很多程序都有记录日志的需求,并且日志中包含的信息既有正常的程序访问日志,还可能有错误.警告等信息输出,python ...
- 优雅地记录Python程序日志1:logging模块简介
本文摘自:https://zhuanlan.zhihu.com/p/31893724 本篇涉及: logging模块的调用: 保存log日志为文件: 调整输入日志等级: 修改日志消息格式: 前言 在使 ...
- Python中的日志记录方案-logging模块&loguru模块
原文链接 原创: 崔庆才 在 Python 中,一般情况下我们可能直接用自带的 logging 模块来记录日志,包括我之前的时候也是一样.在使用时我们需要配置一些 Handler.Formatter ...
- Python之日志处理(logging模块二实战)
实战篇 import logging import logging.handlers LOG_PATH = r'./' def logConfig_1(): ''' 配置 log 输出到文件 : fi ...
随机推荐
- .NET文件跨服务器上传下载
环境说明:两台服务器服务器为A,服务器为B,服务器B为文件服务器 1.在A和B上创建用户docshareuser,用户名和密码保持一致 2.B服务器上设置附件文件夹Attachments共享,添加用户 ...
- HTML input-file 上传类型控制
HTML input-file 上传类型控制 input file 属性 accept 表示可以选择的文件MIME类型,多个MIME类型用英文逗号分开,常用的MIME类型见下表. 只能选择png和gi ...
- PHP取整函数:ceil,floor,round,intval的区别详细解析
floor -- 舍去法取整说明float floor ( float value ) 返回不大于 value 的下一个整数,将 value 的小数部分舍去取整.floor() 返回的类型仍然是 fl ...
- JavaScript的学习1
1.什么是JavaScript? JavaScirpt 它是由网景公司开发的一款基本浏览器.基于面向对象.事件驱动式的网页脚本语言!它的主要应用场景是表单验证.网页特效.一些简单的网页游戏.与服务器进 ...
- [手机取证] Apple Watch取证初探
转载文章请注明出处 1. 关于Apple Watch 苹果公司在2015年3月正式发布了智能手表Apple Watch,包括Apple Watch.Apple Watch Sport以及Apple W ...
- Android性能优化典范
来源:http://hukai.me/android-performance-patterns/#jtss-tsina 0)Render Performance 大多数用户感知到的卡顿等性能问题的最主 ...
- 黑马程序员_ Objective-c 之Foundation笔记(二)
NSArray NSArray的创建 NSArray *array = [NSArray arrayWithObject:@“jack”] 创建单个元素 NSArray *array3 = [NS ...
- 72. Generate Parentheses && Valid Parentheses
Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- pointer on c
http://blog.csdn.net/daniel_ice/article/details/6857019 http://www.cppblog.com/cuigang/archive/2008/ ...
- C#检测网卡和网络统计信息
using System; using System.Collections.Generic; using System.Net.NetworkInformation; public class My ...