_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的更多相关文章

  1. Python 日志打印之自定义logger handler

    日志打印之自定义logger handler By:授客 QQ:1033553122 #实践环境 WIN 10 Python 3.6.5 #实践代码 handler.py #!/usr/bin/env ...

  2. [Python] logging.logger

    <1>. mylogger = logging.getLogger("abc") logging.debug()/logging.info()/logging.warn ...

  3. python自定义pi函数的代码

    下边内容是关于python自定义pi函数的内容. def pi(): # Compute digits of Pi. # Algorithm due to LGLT Meertens. k, a, b ...

  4. Python自定义-分页器

    Python自定义-分页器 分页功能在每个网站都是必要的,对于分页来说,其实就是根据用户的输入计算出应该在数据库表中的起始位置. 1.设定每页显示数据条数 2.用户输入页码(第一页.第二页...) 3 ...

  5. C++进阶--自定义new handler

    //############################################################################ // 自定义new handler /* ...

  6. python自定义mininet拓扑

    python自定义mininet拓扑 前言 闲来无聊,想到很早之前都是用GUI来自定义拓扑,这次用python来自定义一下(以前留下的苦果) 转自Mininet 自定义网络拓扑 过程相对简单 实现过程 ...

  7. 如何使用python自定义命令

    dir.tree.cd等等,都是我们常见的命令.这些命令是开发者开发出来的,如果我们自己想按照自己的想法开发一个命令,应该怎么做呢? 以python语言来实现,问题就是:如何使用python自定义命令 ...

  8. Python自定义线程类简单示例

    Python自定义线程类简单示例 这篇文章主要介绍了Python自定义线程类,结合简单实例形式分析Python线程的定义与调用相关操作技巧,需要的朋友可以参考下.具体如下: 一. 代码     # - ...

  9. python 简单日志框架 自定义logger

    转载请注明: 仰望高端玩家的小清新 http://www.cnblogs.com/luruiyuan/ 通常我们在构建 python 系统时,往往需要一个简单的 logging 框架.python 自 ...

随机推荐

  1. Yii2使用教程

    安装 中文文档:http://www.yiichina.com/doc/guide/2.0/start-installation 1,安装 这里我直接下载归档文件,压缩包安装了.composer各种麻 ...

  2. linux的帮助信息获取以及man章节的划分

    linux的帮助信息获取以及man章节的划分 linux 帮助 man 章节 linux 获取帮助的途径 (1)help (2)man (3)info command在线获取 (4)程序自带帮助文档 ...

  3. 给libpcap增加一个新的捕包方法

    libpcap是一个网络数据包捕获函数库,功能非常强大,提供了系统独立的用户级别网络数据包捕获接口,Libpcap可以在绝大多数类unix 平台下工作.大多数网络监控软件都以它为基础,著名的tcpdu ...

  4. 通过页面调用APP【H5与APP互通】

    现在H5和App原生的内容原来越互通,所涉及的业务也越来越复杂和融合,所以如何互相之间方便的调用才是王道. 场景1 比如用hybrid获取地理位置和短信信息,这当然需要框架封装好,比如利用框架的bri ...

  5. 读javascript高级程序设计17-在线检测,cookie,子cookie

    一.在线状态检测 开发离线应用时,往往在离线状态时把数据存在本地,而在联机状态时再把数据发送到服务器.html5提供了检测在线状态的方法:navigator.onLine和online/offline ...

  6. Mac +WebStorm+nodeJs+Freemarker.js的安装与使用

    第一步用webStorm新建node+express项目 第二步执行npm i –save freemarker.js,安装 freemarker.js 模块 第三步安装java jdk包 jdk包地 ...

  7. git clone error: RPC failed; result=22, HTTP code = 502

    http://www.jianshu.com/p/645d3fe4e028 git克隆的工程太大用https的方式会有如下问题 hbl:tmp hubert$ git clone https://gi ...

  8. git pull 和本地文件冲突问题解决

    具体方法如下 git pull origin 分支 //出现错误 git stash  缓存起来 git pull origin 分支 git stash pop //还原 git stash cle ...

  9. js小时分钟控件--

    直接上代码: var str = ""; document.writeln("<div id=\"_contents\" tabindex=99 ...

  10. windows系统查看被占用的端口号

    我们需要查看80端口被哪个进程占用了, 1. netstat -ano|findstr 80