Python3 logging 模块
Python3 logging模块
日志模块:
用于便捷记录日志且线程安全的模块
CRITICAL = 50
FATAL = CRITICAL
ERROR = 40
WARNING = 30
WARN = WARNING
INFO =20
DEBUG = 10
NOTSET = 0
设置为debug
import logging
import logging
logging.debug('debug')
logging.info('info')
logging.warning('warning')
logging.error('error')
logging.critical('critical') logging.basicConfig(filename = 'access.log',format = '%(asctime)s-%(name)s-%(levelname)s-%(module)s:%(message)s',
datefmt = '%Y-%m-%d %H:%M:%S %p',
level = 10)
logging.debug('debug')
formatter1=logging.Formatter('%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S %p',)
fh1 = logging.FileHandler('test1.log')
fh2 = logging.FileHandler('test2.log')
fh3 = logging.FileHandler('test3.log')
ch = logging.StreamHandler() fh1.setFormatter(formatter1)
fh2.setFormatter(formatter1)
fh3.setFormatter(formatter1)
ch.setFormatter(formatter1) logger1 = logging.getLogger('egon')
logger1.setLevel(10)
logger1.addHandler(fh1)
logger1.addHandler(fh2)
logger1.addHandler(fh3)
logger1.addHandler(ch)
logger1.debug('debug')
logger1.info('info')
logger1.warning('warning')
logger1.critical('critical')
logging配置模板
import os
import logging.config
# 定义三种日志输出格式 开始
standard_format = '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' \
'[%(levelname)s][%(message)s]' #其中name为getlogger指定的名字
simple_format = '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'
id_simple_format = '[%(levelname)s][%(asctime)s] %(message)s'
# 定义日志输出格式 结束
logfile_dir = os.path.dirname(os.path.abspath(__file__)) # log文件的目录
logfile_name = 'all2.log' # log文件名
# 如果不存在定义的日志目录就创建一个
if not os.path.isdir(logfile_dir):
os.mkdir(logfile_dir)
# log文件的全路径
logfile_path = os.path.join(logfile_dir, logfile_name)
# log配置字典
LOGGING_DIC = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': {
'format': standard_format
},
'simple': {
'format': simple_format
},
'id_simple': {
'format': id_simple_format
},
},
'filters': {},
'handlers': {
#打印到终端的日志
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler', # 打印到屏幕
'formatter': 'simple'
},
#打印到文件的日志,收集info及以上的日志
'default': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler', # 保存到文件
'formatter': 'standard',
'filename': logfile_path, # 日志文件
'maxBytes': 1024*1024*5, # 日志大小 5M
'backupCount': 5,
'encoding': 'utf-8', # 日志文件的编码,再也不用担心中文log乱码了
},
'boss': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler', # 保存到文件
'formatter': 'standard',
'filename': 'boss.log', # 日志文件
'maxBytes': 1024 * 1024 * 5, # 日志大小 5M
'backupCount': 5,
'encoding': 'utf-8', # 日志文件的编码,再也不用担心中文log乱码了
},
},
'loggers': {
#logger1=logging.getLogger(__name__)拿到的logger配置
'': {
'handlers': ['default', 'console'], # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕
'level': 'DEBUG',
'propagate': True, # 向上(更高level的logger)传递
},
#logger1=logging.getLogger('collect')拿到的logger配置
'collect': {
'handlers': ['boss',], # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕
'level': 'DEBUG',
'propagate': True, # 向上(更高level的logger)传递
},
},
}
def load_my_logging_cfg():
logging.config.dictConfig(LOGGING_DIC) # 导入上面定义的logging配置
# logger = logging.getLogger(__name__) # 生成一个log实例
# logger.info('It works!') # 记录该文件的运行状态
if __name__ == '__main__':
load_my_logging_cfg()
调用 logging 配置模板
import logging
import my_log_settings
my_log_settings.load_my_logging_cfg() logger1=logging.getLogger(__name__)
logger2=logging.getLogger('collect') logger1.debug('默认日志的debug')
logger2.debug('给老板一封信')
logger2.debug('给前台MM一封信')
Python3 logging 模块的更多相关文章
- Python3 logging模块&ConfigParser模块
''' 博客园 Infi_chu ''' ''' logging模块 该模块是关于日志相关操作的模块 ''' import logging # logging.debug('debug') # log ...
- python3 logging模块
很多程序都有记录日志的需求,并且日志包含的信息有正常的程序访问日志还可能有错误,警告等信息输出,python的logging模块提供了标准的日志接口,可以通过它存储各种格式的日志,日志级别等级:cri ...
- python3:logging模块 输出日志到文件
python自动化测试脚本运行后,想要将日志保存到某个特定文件,使用python的logging模块实现 参考代码: import logging def initLogging(logFilenam ...
- Python3.x:logging模块对运行过程记录
Python3.x:logging模块对运行过程记录 示例: import logging # 设置 logger = logging.getLogger() #set loghandler #默认路 ...
- Python3之logging模块浅析
Python3之logging模块浅析 目录 Python3之logging模块浅析 简单用法 日志与控制台同时输出 一个同时输出到屏幕.文件的完成例子 日志文件截取 日志重复打印问题解决 问题分 ...
- python3之xml&ConfigParser&hashlib&Subprocess&logging模块
1.xml模块 XML 指可扩展标记语言(eXtensible Markup Language),标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言. XML 被设计用来传输和存储 ...
- python3学习-logging模块
1.logging模块的使用非常简单,引入模块就可以使用. import logging logging.debug('This is debug message') logging.info('Th ...
- s14 第5天 时间模块 随机模块 String模块 shutil模块(文件操作) 文件压缩(zipfile和tarfile)shelve模块 XML模块 ConfigParser配置文件操作模块 hashlib散列模块 Subprocess模块(调用shell) logging模块 正则表达式模块 r字符串和转译
时间模块 time datatime time.clock(2.7) time.process_time(3.3) 测量处理器运算时间,不包括sleep时间 time.altzone 返回与UTC时间 ...
- python+selenium自动化软件测试(第9章) :Logging模块
9.1 Logging模块 什么是日志记录?记录是跟踪运行时发生的事件的一种手段.该软件的开发人员将记录调用添加到其代码中,以指示某些事件已发生.事件由描述性消息描述,该消息可以可选地包含可变数据(即 ...
随机推荐
- ASP.NET CORE RAZOR :向 Razor 页面应用添加模型
本文来自:https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/razor-pages/model 在本部分中将添加用于管理数据库中的电影的类. ...
- OrCAD16.6中对比两份DSN文件的方法
OrCAD16.6中对比两份改版前后DSN文件的方法 两种方法: (1)第一种用软件对比netlist (2)用orcad自带的对比功能 一.将两份要对比的原理图都生成orTelesis.dll格式的 ...
- C/C++ 内存管理总结
C内存管理 存储时: 执行程序在存储时(没有调入到内存)分为代码区(text).数据区(data)和未初始化数据区(bss)3个部分. 1 代码区(text segment) 存放CPU执行的机器指令 ...
- Linux Suse 查看wwn号码的方法
查看wwn号码 cat /sys/class/fc_host/host*/port_name *代表全部host目录
- Vim使用个人心得
个人最近在Windows上使用gVim 1.移动光标 h,j,k,l 键为左,下,上,右,方向键,控制光标移动,插入状态下不可用,插入状态下,按V键进入查看状态,可使用. 2.进入编辑模式:按 i 键 ...
- MapReduce Input Split 输入分/切片
MapReduce Input Split(输入分/切片)详解 public static long getMaxSplitSize(JobContext context) { return cont ...
- 【Selenium + Python】之OSError: [WinError 6] 句柄无效。
问题描述:执行多个用例的时候,会抛出异常: Traceback (most recent call last): File "F:\Demo\pomGisStu\gis\test_case\ ...
- cordova ios升级插件
org.ssgroup.sope.cordova.upgrade 支持强制升级与选择升级 插件已经开源在https://github.com/shenshouer/org.ssgroup.sope.c ...
- Spring Cloud 微服务五:Spring cloud gateway限流
前言:在互联网应用中,特别是电商,高并发的场景非常多,比如:秒杀.抢购.双11等,在开始时间点会使流量爆发式地涌入,如果对网络流量不加控制很有可能造成后台实例资源耗尽.限流是指通过指定的策略削减流量, ...
- volley全然解析
一.volley是什么? 1.简单介绍 Volley是Goole在2013年Google I/O大会上推出了一个新的网络通信框架,它是开源的.从名字由来和配图中无数急促的火箭能够看出 Volley ...