python 日志不会按照日期分割_python实现日志按天分割
本文实例为大家分享了python实现日志按天分割的具体代码,供大家参考,具体内容如下
日志格式:
1.1.1.1 - - [30/Apr/2015:00:34:55 +0800] “POST /iDataService/services/MemRoomService HTTP/1.0” 200 405 “-” “Axis/1.4” “-”
1.1.1.1 - - [30/Apr/2015:00:34:55 +0800] “POST /iDataService/services/CutLoginService HTTP/1.1” 200 438 “-” “Apache CXF 2.7.8” “-”
1.1.1.1 - - [20/Apr/2015:00:34:55 +0800] “POST /iDataService/services/NoticeListService HTTP/1.1” 200 656 “-” “Apache CXF 2.7.8” “-”
1.1.1.1 - - [30/Apr/2016:00:34:56 +0800] “POST /iDataService/services/MemSelfQueryService HTTP/1.0” 200 1344 “-” “Axis/1.4” “-“
分割要求:
对日子进行按天分割,文件名称如access.log-20160101
#!/usr/bin/env python
# -- conding:utf-8 --
#
import os
import time
path1='/lianxi/python/split/lianxi1'
file=open(path1,'r')
for line in file:
str=line.split()[3]
otime=str[1:12]
time_jieshu = time.strptime(otime, '%d/%b/%Y')
time_jieshu = int(time.mktime(time_jieshu))
ntime=time.strftime('%Y%m%d',time.localtime(time_jieshu))
# print ntime
log_file='/lianxi/python/split/access.log-%s' %ntime
with open(log_file,'a') as f:
if not os.path.exists(log_file):
os.mknod(log_file)
# f.write(line)
# else:
f.write(line)
f.close()
#!/usr/bin/python
#coding=utf-8
#author lyk
import re,os,commands
a=open('/python/access.log','r')
exc_month={'Apr':'04','Aug':'08','Dec':'12','Feb':'02','Jan':'01','Jul':'07','Jun':'06','Mar':'03','May':'05','Nov':'11','Oct':'10','Sep':'09'}
def touch_file():
blist=[]
commands.getoutput('rm -rf /accesslog/*')
for i in a:
blist.append(re.findall(r"\[(.+?):",i)[0])
new_blist=[]
for j in blist:
if j.replace('/','.') not in new_blist:
new_blist.append(j.replace('/','.'))
for j in new_blist:
commands.getoutput('touch /accesslog/access.log-%s'%(j[7:17]+exc_month[j[3:6]]+j[0:2]))
def append_file():
for i in a:
mfile=re.findall(r"\[(.+?):",i)[0].replace('/','')
f=open("/accesslog/access.log-"+mfile[5:9]+exc_month[mfile[2:5]]+mfile[0:2],'a')
f.write(i)
f.close()
def tar_file():
for i in commands.getoutput('ls /accesslog').splitlines():
commands.getoutput("gzip /accesslog/%s"%i)
if __name__=='__main__':
touch_file()
append_file()
tar_file()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
python 日志不会按照日期分割_python实现日志按天分割相关推荐
- python logging日志分割_python logging日志模块以及多进程日志
本篇文章主要对 python logging 的介绍加深理解.更主要是 讨论在多进程环境下如何使用logging 来输出日志, 如何安全地切分日志文件. 1. logging日志模块介绍 python ...
- python将object转换成日期型_python – Pandas:将dtype’object’转换为int
我已经在Pandas中读取了一个SQL查询,并且这些值以dtype'object'形式出现,尽管它们是字符串,日期和整数.我能够将日期'对象'转换为Pandas datetime dtype,但是在尝 ...
- 日志输出到文件nacos 配置_python 配置日志输出到终端与文件
python 日志输出到终端与文件配置 Pou光明 2018-12-15 大家好,那个熟悉的我又回来了~ 最近一段时间由于工作原因,用了一段时间python.接触到了坑爹的GIL锁.python日志, ...
- python日志模块_Python之日志处理(logging模块)
转载自:https://www.cnblogs.com/yyds/p/6901864.html 本节内容 日志相关概念 logging模块简介 使用logging提供的模块级别的函数记录日志 logg ...
- python日志内容存储到文件里_Python中如何实现将logging日志保存到文件中
Python中如何实现将logging日志保存到文件中 发布时间:2020-11-09 17:03:14 来源:亿速云 阅读:66 今天就跟大家聊聊有关Python中如何实现将logging日志保存到 ...
- python大型项目中的日志模块_Python中日志模块的使用
前言 程序和脚本往往是无人值守运行的,一旦发生问题,就需要我们去追溯当时的情况来定位问题的原因. 这便需要我们在程序和脚本中引入日志的功能. 相比于print信息,使用logging日志有以下优点 可 ...
- python日志处理_Python之日志处理(logging模块)
转发:https://www.cnblogs.com/yyds/p/6901864.html 本节内容 日志相关概念 logging模块简介 使用logging提供的模块级别的函数记录日志 loggi ...
- python多进程打印输出_python 多进程日志 logging
python的logging模块提供了灵活的标准模块,使得任何Python程序都可以使用这个第三方模块来实现日志记录. 但是 python 中logging 并不支持多进程,所以会遇到不少麻烦. 以 ...
- python写日志文件_Python logging日志模块 配置文件方式
在一些微服务或web服务中我们难免需要日志功能,用来记录一些用户的登录记录,操作记录,以及一些程序的崩溃定位,执行访问定位等等; Python内置 非常强大的日志模块 ==> logging 今 ...
最新文章
- pandas对dataframe的数据行(rows)进行随机抽样:使用sample函数进行数据行(rows)随机抽样
- pip和conda批量导出、安装第三方依赖库(requirements.txt)
- C++Builder中使用Pas文件
- 认识JSON绑定:概述系列
- 圆柱与平面接触宽度_好烦!这个建筑高大斜圆柱真难施工!别怕!学会这种工法就不难了...
- 解决 Electron 包下载太慢问题
- PAT 1032 (未完成)
- Win Phone 8 Emulator启动错误:提示无法创建虚拟机,Xde找不到主机的IPv4地址
- 哈工大SCIR Lab | EMNLP 2019 结合单词级别意图识别的stack-propagation框架进行口语理解...
- 国外项目跳板机访问时差问题
- C#实现简单的网页爬虫
- Python+OpenCV3.3图像处理视频教程-贾志刚-专题视频课程
- 有线网络、无线网络、蜂窝网络、移动网络到底是啥?
- JAVA实现负载均衡
- Java中的Constants类
- 酵素果冻真的能减肥吗?
- 浅谈社群运营的2种抽奖玩法
- 在家访问办公室电脑能实现吗?远程控制办公室电脑的软件推荐
- 汉画轩国学传承国学精髓 解惑修身之道
- linux shadow文件*,Linux怎么查找shadow文件进入这样几步轻松搞定