MySQL

做最专业的DBA

Python 日志分割


数据增量更新的python脚本记录的日志文件越来越大,所有需要拆分,google了下,找到了例子,不过有个bug就是日志信息已经录入了日志文件了,也拆分了,但是执行python脚本的控制台上会原样打印出日志信息。


python案例代码

[huayuan] /data0/production_api/split_new/process$ more p6.py
import time  
import logging  
import logging.handlers  

logging.basicConfig()  

myapp = logging.getLogger('myapp')  
myapp.setLevel(logging.INFO)  

filehandler = logging.handlers.TimedRotatingFileHandler("logs/myapp.log", when='S', interval=3, backupCount=10)   
filehandler.suffix = "%Y-%m-%d_%H-%M-%S.log"  
myapp.addHandler(filehandler)  

for i in range(10):  
    #time.sleep(0.1)  
    time.sleep(1)
    myapp.info(str(i)+",test")  
[huayuan] /data0/production_api/split_new/process$ 

[huayuan] /


执行过程,界面上会有日志信息输出

[huayuan] /data0/production_api/split_new/process$ python p6.py
INFO:myapp:0,test
INFO:myapp:1,test
INFO:myapp:2,test
INFO:myapp:3,test
INFO:myapp:4,test
INFO:myapp:5,test
INFO:myapp:6,test
INFO:myapp:7,test
INFO:myapp:8,test
INFO:myapp:9,test
[huayuan] 


而日志文件也同时写入,且被拆分掉:

[huayuan] /data0/production_api/split_new/process$ ll logs/*
-rw-r--r-- 1 laobanzhang wheel 14 Aug 31 17:47 logs/myapp.log
-rw-r--r-- 1 laobanzhang wheel 14 Aug 31 17:47 logs/myapp.log.2017-08-31_17-47-30.log
-rw-r--r-- 1 laobanzhang wheel 21 Aug 31 17:47 logs/myapp.log.2017-08-31_17-47-33.log
-rw-r--r-- 1 laobanzhang wheel 21 Aug 31 17:47 logs/myapp.log.2017-08-31_17-47-36.log
[huayuan] /data0/production_api/split_new/process$ 


问题处理,google上查阅了下TimedRotatingFileHandler,没有找到方法,想到一个临时解决办法 >/dev/null

[huayuan] /data0/production_api/split_new/process$ rm -rf logs/*
[huayuan] /data0/production_api/split_new/process$ python p6.py 2> /dev/null
[huayuan] /data0/production_api/split_new/process$ ll logs/*
-rw-r--r-- 1 laobanzhang wheel 14 Aug 31 17:49 logs/myapp.log
-rw-r--r-- 1 laobanzhang wheel 14 Aug 31 17:49 logs/myapp.log.2017-08-31_17-49-01.log
-rw-r--r-- 1 laobanzhang wheel 21 Aug 31 17:49 logs/myapp.log.2017-08-31_17-49-04.log
-rw-r--r-- 1 laobanzhang wheel 21 Aug 31 17:49 logs/myapp.log.2017-08-31_17-49-07.log
[huayuan] /data0/production_api/split_new/process$ more logs/myapp.log.2017-08-31_17-49-07.log
5,test
6,test
7,test
[huayuan] /data0/production_api/split_new/process$ 

参考文章地址:https://docs.python.org/2/library/logging.handlers.html

阅读更多

扫码向博主提问

去开通我的Chat快问

mchdba

博客专家

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • MySQL Hbas
  • Hbase
  • Tidb
  • cdb
  • 数据拆分
个人分类: - - - Python
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭