1.升级优化,提供用户自定义日志level文件夹生成控制,提供日志错误显示到日志打印异常补获到日志

# coding=utf-8
import logging
import time
import os
import logging.handlers def logger(appName,rootstdout=True,handlerList=None): log_fmt= "%(asctime)s --%(name)s [%(levelname)s]:\n%(message)s"
c_fmt="%(asctime)s --%(name)s [%(levelname)s]:\n%(message)s"
date_format = "%Y-%m-%d %H:%M:%S %a"
#设置Console输出level
logging.basicConfig(level=logging.DEBUG,
format=c_fmt,
datefmt=date_format, ) levels=[]
if isinstance(handlerList,list):
if handlerList.__contains__("I") or handlerList.__contains__("Info"):
levels.append("Info")
if handlerList.__contains__("D") or handlerList.__contains__("Debug"):
levels.append("Debug")
if handlerList.__contains__("E") or handlerList.__contains__("Error"):
levels.append("Error")
if handlerList.__contains__("W") or handlerList.__contains__("Warning"):
levels.append("Warning")
if levels:
stamp = time.strftime("%Y%m%d", time.localtime())+".log"
logsdir=os.path.join(os.getcwd(),"logs")
if os.path.exists(logsdir):
for p in levels:
if os.path.exists(os.path.join(logsdir,p)):
pass
else:
os.mkdir(os.path.join(logsdir,p))
else:
os.mkdir(logsdir)
for p in levels:
print(os.path.join(logsdir,p))
os.mkdir(os.path.join(logsdir,p)) f_dict={}
for i in levels:
filename=os.path.join(logsdir,i,stamp)
f_dict[i]=filename
logger= logging.getLogger(appName) for k,v in f_dict.items():
handler=logging.handlers.RotatingFileHandler(filename=v, maxBytes=1024*1024*50, backupCount=5,encoding="utf-8",delay=False)
h_fmt=logging.Formatter(log_fmt)
handler.setFormatter(h_fmt)
if k=="E" or k=="Error":
handler.setLevel(logging.ERROR)
elif k=="I" or k=="Info":
handler.setLevel(logging.INFO)
elif k== "W" or k=="Warning":
handler.setLevel(logging.WARNING)
elif k=="D" or k=="Debug":
handler.setLevel(logging.DEBUG)
else:
raise NameError("check your logLevel Name is corrected or not")
logger.addHandler(handler)
logger.propagate = rootstdout
return logger
else:
raise TypeError("check handlerList at least one corrected logLevel in:'Error','Info','Warning','Debug'")
else:
raise NameError("handlerList expected type is list but get type {}".format(type(handlerList).__name__))
if __name__ == "__main__":
logger=logger("root",rootstdout=True,handlerList=['I']) time.sleep(0.01)
logger.info("file test",exc_info=True)
logger.debug("bebug test")
logger.error("error test")
logger.warning("warning test")

  

优化升级logging封装RotatingFileHandler的更多相关文章

  1. 【百度地图API】今日小年大进步,齐头共进贺佳节——API优化升级上线,不再增加内存消耗

    原文:[百度地图API]今日小年大进步,齐头共进贺佳节--API优化升级上线,不再增加内存消耗 任务描述: 今天是2011年01月26日,小年夜.百度地图API在小年夜献给广大API爱好者一份给力的礼 ...

  2. 【python】实用的logging封装

    #!/usr/bin/python import logging import logging.handlers def set_logger(filename, logmod): log_size ...

  3. 一种logging封装方法,不会产生重复log

    在调试logging的封装的时候,发现已经调用了logging封装的函数,在被其它函数再调用时,会出现重复的logging.原因是不同的地方创建了不同的handler,所以会重复,可以使用暴力方法解决 ...

  4. MyCms 自媒体 CMS 系统 v2.6,SEO 优化升级

    MyCms 是一款基于Laravel开发的开源免费的自媒体博客CMS系统,助力开发者知识技能变现. MyCms 基于Apache2.0开源协议发布,免费且不限制商业使用,欢迎持续关注我们. V2.6 ...

  5. 升级优化关于日志生成logging封装TimedRotatingFileHandler

    1.变更升级:优化日志自定义输出到文件的level,以及文件夹生成用户自由控制 # coding=utf-8 import logging import time import os import l ...

  6. 异构(兼容dubbo)SOA系统架构(.net)优化升级

    前面一片文章已经提到我司的异构(兼容dubbo)SOA系统架构,解决了不少技术痛点,也还算比较完善,也顺利推广开来. 但作为项目的开发者,自己产品的问题心里是清楚的,离自己满意还是有不小的距离. 在推 ...

  7. oa办公系统快速开发工具,助力企业优化升级

    随着互联网的快速发展.信息化 IT 技术的不断进步.移动互联新技术的兴起,不管是大的集团企业还是中小型企业,纸质化的办公模式已不能满足现有需求,构建oa平台,为员工提供高效的办公环境尤其重要. 我们先 ...

  8. Synchronized用法原理和锁优化升级过程(面试)

    简介 多线程一直是面试中的重点和难点,无论你现在处于啥级别段位,对synchronized关键字的学习避免不了,这是我的心得体会.下面咱们以面试的思维来对synchronized做一个系统的描述,如果 ...

  9. ES 集群上,业务单点如何优化升级?

    摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! ES 基础 ES 集群 ES 集群上业务优化 一.ES 基础 ...

随机推荐

  1. CentOS 7 搭建PXC 数据库集群

    CentOS 7 搭建PXC 数据库集群 PXC( Percona XtraDB Cluster ) 特点如下: 1.同步复制,事务要么在所有节点提交或不提交,保证了数据的强一致性. 2.多主复制,可 ...

  2. nginx的MainLine version、Stable version、Legacy versions

    Nginx的版本说明Mainline version:在线版本,正处于开发状态Stable version :稳定版本(一般下载使用)Legacy version :遗留版本,遗留的老的版本 Linu ...

  3. 纯css修改复选框默认样式

    input[type='checkbox']{ width: 20px; height: 20px; background-color: #fff; -webkit-appearance:none; ...

  4. axTOCControl右键

    private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e) { if (e. ...

  5. 封装一个 TopBarBaseActivity

    什么是快速开发嘞,看这个效果 然而我只用了这么几行代码: activity_main.xml 里面什么也没有! 其实说白了哈,就是我把 TopBar 封装在 TopBarBaseActivity 里面 ...

  6. PHP获取用户的真实IP地址

    本文出至:新太潮流网络博客 PHP获取用户的真实IP地址,非代理IP function getClientIP(){ global $ip; if(getenv("HTTP_CLIENT_I ...

  7. [翻译] CotEditor

    CotEditor https://github.com/coteditor/CotEditor CotEditor is a lightweight plain-text editor for OS ...

  8. 微信自定义菜单的emoji图标

    微信公众号自定义菜单添加emoji表情图标 第一步:打开微信公众平台接口调试工具,点击前往接口调试工具: 第二步:把这段代码  {"button":[{"sub_butt ...

  9. The expression of type Integer is unboxed into int

    问题:The expression of type Integer is unboxed into int 原因:java的包装类,方法里面要的是Integer,传入的参数确实int类型 解决方案: ...

  10. 铁乐学python_Day42_线程池

    铁乐学python_Day42_线程池 concurrent.futures 异步调用模块 concurrent.futures模块提供了高度封装的异步调用接口 ThreadPoolExecutor: ...