logging日志类
2024-08-30 13:01:50
#! /usr/bin/env python
#coding=utf-8
import logging,os class Logger:
def __init__(self, path,clevel = logging.DEBUG,Flevel = logging.DEBUG):
self.logger = logging.getLogger(path)
self.logger.setLevel(logging.DEBUG)
fmt = logging.Formatter('[%(asctime)s] [%(levelname)s] %(message)s', '%Y-%m-%d %H:%M:%S')
#设置CMD日志
sh = logging.StreamHandler()
sh.setFormatter(fmt)
sh.setLevel(clevel)
#设置文件日志
fh = logging.FileHandler(path)
fh.setFormatter(fmt)
fh.setLevel(Flevel)
self.logger.addHandler(sh)
self.logger.addHandler(fh) def debug(self,message):
self.logger.debug(message) def info(self,message):
self.logger.info(message) def war(self,message):
self.logger.warning(message) def error(self,message):
self.logger.error(message) def cri(self,message):
self.logger.critical(message) if __name__ =='__main__':
log_mu = Logger('file_path',logging.DEBUG,logging.DEBUG)
# log_mu.debug('一个debug信息')
log_mu.info('一个info信息')
# log_mu.war('一个warning信息')
# log_mu.error('一个error信息')
# log_mu.cri('一个致命critical信息') # 读日志文件的时候必须用gbk方式解码
# with open('file_path','r',encoding='gbk') as f:
# print(f.read().strip())
最新文章
- 使用weave管理docker网络
- html5 定位 获得当前位置的经纬度
- Practical JAVA(二)关于对象的类型和equals函数
- Linux编辑器vi使用方法详细介绍
- WCF的行为与异常-------配置文件说明
- iOS OC开发代码规范
- Pycharm小技巧--使用正则进行查找和批量替换
- jvm调优-从eclipse开始
- GGS-DDU HDU - 4966
- MySQL匹配指定字符串的查询
- 微信小程序之wx.request:fail错误,真机预览请求无效问题解决,安卓,ios网络预览异常
- Linux 的虚拟文件系统(强烈推荐)
- ES6学习笔记(二):引用数据类型
- ERP渠道文档管理(二十四)
- python-day9-数据类型总结
- LeetCode 318. Maximum Product of Word Lengths (状态压缩)
- Linux C single linked for any data type
- js中为什么非要alert一下下一步才会执行
- django视图函数中 应用装饰器
- ballerina 学习四 如何进行项目结构规划
热门文章
- magic packet 远程唤醒需填写 IP broadcast address
- 出现异常时直接把e输出比输出e.getMessage()好得多
- NHibernate之旅(7):初探NHibernate中的并发控制
- docker 搭建linux samba
- ISC DHCP: Enterprise grade solution for configuration needs
- 常用DOS下MSC指令
- HBase协处理器同步二级索引到Solr
- spark mongo 性能优化
- jquery.validate ajax验证
- VC实现趋势图绘制