当前位置:网站首页>logging 学习最终版-配置的不同级别日志打印的颜色
logging 学习最终版-配置的不同级别日志打印的颜色
2022-07-26 10:50:00 【亚索不会吹风】
import logging
#设置日志颜色的包
import colorlog
import datetime
''' Loggers:记录器,提供应用程序代码能直接使用的接口; Handlers:处理器,将记录器产生的日志发送至目的地; Filters:过滤器,提供更好的粒度控制,决定哪些日志会被输出; Formatters:格式化器,设置日志内容的组成结构和消息字段。 %(name)s Logger的名字 #也就是其中的.getLogger里的路径,或者我们用他的文件名看我们填什么 %(levelno)s 数字形式的日志级别 #日志里面的打印的对象的级别 %(levelname)s 文本形式的日志级别 #级别的名称 %(pathname)s 调用日志输出函数的模块的完整路径名,可能没有 %(filename)s 调用日志输出函数的模块的文件名 %(module)s 调用日志输出函数的模块名 %(funcName)s 调用日志输出函数的函数名 %(lineno)d 调用日志输出函数的语句所在的代码行 %(created)f 当前时间,用UNIX标准的表示时间的浮 点数表示 %(relativeCreated)d 输出日志信息时的,自Logger创建以 来的毫秒数 %(asctime)s 字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒 %(thread)d 线程ID。可能没有 %(threadName)s 线程名。可能没有 %(process)d 进程ID。可能没有 %(message)s用户输出的消息 '''
'''日志颜色配置'''
log_colors_config = {
#颜色支持 blue蓝,green绿色,red红色,yellow黄色,cyan青色
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'bold_red',
}
'''创建logger记录器'''
logger = logging.getLogger('test')
# 输出到控制台
console_handler = logging.StreamHandler()
# 输出到文件
path = "./log"
'''获取当前年月日作为日志文件名'''
fileName = str(datetime.datetime.now().year) + '-' + str(datetime.datetime.now().month) + '-' + str(
datetime.datetime.now().day) + '.log'
file_handler = logging.FileHandler(filename=path+'\\'+fileName, mode='a', encoding='utf8')
'''日志级别设置'''
#logger控制最低输出什么级别日志(优先级最高)
logger.setLevel(logging.DEBUG)
#console_handler设置控制台最低输出级别日志
console_handler.setLevel(logging.DEBUG)
#console_handler设置保存到文件最低输出级别日志
file_handler.setLevel(logging.INFO)
# 日志输出格式
#保存文件的日志格式
file_formatter = logging.Formatter(
fmt='[%(asctime)s] %(filename)s -> %(funcName)s line:%(lineno)d [%(levelname)s] : %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
#控制台的日志格式
console_formatter = colorlog.ColoredFormatter(
#输出那些信息,时间,文件名,函数名等等
fmt='%(log_color)s[%(asctime)s] %(filename)s -> %(funcName)s line:%(lineno)d [%(levelname)s] : %(message)s',
#时间格式
datefmt='%Y-%m-%d %H:%M:%S',
log_colors=log_colors_config
)
console_handler.setFormatter(console_formatter)
file_handler.setFormatter(file_formatter)
# 重复日志问题:
# 这里进行判断,如果logger.handlers列表为空,则添加,否则,直接去写日志,解决重复打印的问题
if not logger.handlers:
logger.addHandler(console_handler)
logger.addHandler(file_handler)
console_handler.close()
file_handler.close()
if __name__ == '__main__':
logger.debug('颜色')
logger.info('绿色测试日志保存路径{}'.format(path+fileName))
logger.warning('颜色')
logger.error('error')
logger.critical('critical')
边栏推荐
- The assignment of member pointer defined in C structure and the use of structure pointer as member function parameter
- Bash shell学习笔记(七)
- 在神州IV开发板上为STemWin 5.22加入触屏驱动
- 很多人都不清楚自己找的是Kanban软件还是看板软件
- 企鹅龙(DRBL)无盘启动+再生龙(clonezilla)网络备份与还原系统
- Common classes (understand)
- 35. 搜索插入位置
- Shell script fails to execute repeatedly automatically
- Sql Server之查询总结
- ESXi6.5补丁更新
猜你喜欢

PLC与伺服电机连接

看源码之LinkedList

二叉树的遍历 递归+迭代

RT thread learning notes (III) -- building a compilation environment with scons
![[leetcode daily question 2021/8/31] 1109. Flight reservation statistics [medium] differential array](/img/9d/5ce5d4144a9edc3891147290e360d8.png)
[leetcode daily question 2021/8/31] 1109. Flight reservation statistics [medium] differential array

Kali view IP address

WIRESHARK基础教程以太帧的分析。

Add touch screen driver for stemwin 5.22 on Shenzhou IV development board

Bash shell学习笔记(一)

0x00007FFD977C04A8 (Qt5Sqld.dll)处(位于 a.exe 中)引发的异常: 0xC0000005: 读取位置 0x0000000000000010 时发生访问冲突
随机推荐
Flutter报错 Incorrect use of ParentDataWidget When the exception was thrown, this was the stack:
Flutter jni混淆 引入.so文件release包闪退
Sql Server 数据库之初学体验
Pengge C language 20210811 program structure operation
RT thread learning notes (VIII) -- start the elmfat file system based on SPI flash (Part 2)
菜鸟看源码之LinkedBlockingQueue
在神州IV开发板上为STemWin 5.22加入触屏驱动
Stringing of macro parameters and connection of macro parameters in C language
shell 脚本 失败自动重复执行
Sql Server之查询总结
Pengge C language lesson 4 (3)
The assignment of member pointer defined in C structure and the use of structure pointer as member function parameter
mysql20210906
232. Implement queue with stack
二叉树的遍历 递归+迭代
Bigdecimal的加减乘除、比较大小、向上向下取整 和 Bigdecimal的集合累加、判断BigDecimal是否有小数
Happens-Before原则深入解读
[leetcode daily question 2021/5/8]1723. The shortest time to complete all work
使用flex实现左中右布局,中间自适应
Newbie sees the source code arraydeque