python自定义logger handler
_filefmt=os.path.join("logs","%Y-%m-%d.log")
class MyLoggerHandler(logging.Handler):
def __init__(self,filefmt=None):
self.filefmt=filefmt
if filefmt is None:
self.filefmt=_filefmt
logging.Handler.__init__(self)
def emit(self,record):
msg=self.format(record)
_filePath=datetime.datetime.now().strftime(self.filefmt)
_dir=os.path.dirname(_filePath)
try:
if os.path.exists(_dir) is False:
os.makedirs(_dir)
except Exception:
print("can not make dirs")
print("filepath is "+_filePath)
pass
try:
_fobj=open(_filePath,'a')
_fobj.write(msg)
_fobj.write("\n")
_fobj.flush()
_fobj.close()
except Exception:
print("can not write to file")
print("filepath is "+_filePath)
pass
if __name__ == '__main__':
logging.basicConfig()
logger = logging.getLogger("logger")
logger.setLevel(logging.INFO)
filehandler = MyLoggerHandler()
logger.addHandler(filehandler)
logger.info('log...')
如上,实现每天一个日志文件。
python自定义logger handler的更多相关文章
- Python 日志打印之自定义logger handler
日志打印之自定义logger handler By:授客 QQ:1033553122 #实践环境 WIN 10 Python 3.6.5 #实践代码 handler.py #!/usr/bin/env ...
- [Python] logging.logger
<1>. mylogger = logging.getLogger("abc") logging.debug()/logging.info()/logging.warn ...
- python自定义pi函数的代码
下边内容是关于python自定义pi函数的内容. def pi(): # Compute digits of Pi. # Algorithm due to LGLT Meertens. k, a, b ...
- Python自定义-分页器
Python自定义-分页器 分页功能在每个网站都是必要的,对于分页来说,其实就是根据用户的输入计算出应该在数据库表中的起始位置. 1.设定每页显示数据条数 2.用户输入页码(第一页.第二页...) 3 ...
- C++进阶--自定义new handler
//############################################################################ // 自定义new handler /* ...
- python自定义mininet拓扑
python自定义mininet拓扑 前言 闲来无聊,想到很早之前都是用GUI来自定义拓扑,这次用python来自定义一下(以前留下的苦果) 转自Mininet 自定义网络拓扑 过程相对简单 实现过程 ...
- 如何使用python自定义命令
dir.tree.cd等等,都是我们常见的命令.这些命令是开发者开发出来的,如果我们自己想按照自己的想法开发一个命令,应该怎么做呢? 以python语言来实现,问题就是:如何使用python自定义命令 ...
- Python自定义线程类简单示例
Python自定义线程类简单示例 这篇文章主要介绍了Python自定义线程类,结合简单实例形式分析Python线程的定义与调用相关操作技巧,需要的朋友可以参考下.具体如下: 一. 代码 # - ...
- python 简单日志框架 自定义logger
转载请注明: 仰望高端玩家的小清新 http://www.cnblogs.com/luruiyuan/ 通常我们在构建 python 系统时,往往需要一个简单的 logging 框架.python 自 ...
随机推荐
- localStorage 2016/12/26
在HTML5中,新加入了一个localStorage特性,这个特性主要是用来作为本地存储来使用的,解决了cookie存储空间不足的问题(cookie中每条cookie的存储空间为4k),localSt ...
- html/css 浮动练习之井字形布局
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Web自动化基础(一)使用Selenium定位元素
什么是元素?我们知道网页上有什么内容显示出来,比如一个按钮,一个输入框,一张图片,都可以理解成元素,这些元素是由html代码构成的,比如图片可以用<img>标签来展示,一个输入框可以用&l ...
- VGA, QVGA, HVGA, WVGA, FWVGA和iPhone显示分辨率
首先这些都是说的屏幕显示分辨率 VGA (Video Graphics Array), 分辨率为 480*640. QVGA (Quarter VGA), 分辨率为240*320. HVGA (Hal ...
- java中调用xml的方法:DocumentBuilderFactory
具体的使用方法如下: (1)得到 DOM 解析器的工厂实例 DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance(); (2 ...
- Linux 下使用Visual Studio Code
1.下载:https://az764295.vo.msecnd.net/stable/db71ac615ddf9f33b133ff2536f5d33a77d4774e/VSCode-linux-x64 ...
- js控制键盘只能输入数字和退格键,delete键
function numbText(e){ if(e&& e.stopPropagation){ code= e.which; }else{ code= window.event.ke ...
- asp.net自己创建的app_code文件夹中的类不能访问的解决办法
在Web应用程序中不能通过右键项目-〉”添加“-〉”添加ASP.NET文件夹“方式添加 .因为Web应用程序中App_Code就不存在 .不过可以通过手动的方式创建,添加一个文件夹命名为App_Cod ...
- Nginx中的进程亲和性 affinity
Nginx采用多进程Master/Worker结构,Worker进程数为CPU个数时工作效率最高,Nginx通过affinity为每个Worker进程绑定一个CPU,避免进程切换带来的消耗,同时能够保 ...
- C#如何获取项目中的其他文件夹的路径
//一般用string p=AppDomain.CurrentDomain.BaseDirectory+"\\其他"; //其它的还有 string str1 =Process.G ...