记录一次利用 python 进行日志模块开发过程
只记录大体思路和我认为其中需要记录的地方。
正则匹配
正则匹配的模式很难记忆,即使记住了,也很难写出无错误的匹配模式。但是,借助网上一些提供实时对比的网站,如 regexr.com。
代码示意:
import os
import re
'''
define regex mode
'''
get_imgpath_regex = re.compile(r'''(
(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2},\d+) # time
\s # separate
(.*)?get_imgpath\sused\stime\sis\s # info
(\d+.\d+) # time
\sargs\sis\s\(u' # separate
(.*?)',\)\sresult\sis\s # img dir
(.*?.jpg) # img path
)''', re.VERBOSE)
get_imgpath_flag = 'get_imgpath'
def main():
for infile in infile_list:
# prase line using regex mode
with open(infile, "r") as file:
for line in file: # 先判断关键词
currentDict = {}
if get_imgpath_flag in line:
for groups in regex['get_imgpath_regex'].findall(line): # 再提取模式对应的内容
currentDict = {'date': groups[1], 'cost_time':groups[3],
'img_dir':groups[4], 'img_path':groups[5]}
# print(currentDict)
get_imgpath_match.append(currentDict)
else:
pass
参数配置
采取的方式为参数存储在一个单独的文件,如 config.json。
{
"FLAG" : {
"SAVE_SPILT_LOG_FILE_FLAG" : false ,
"SAVE_MERGE_LOG_FILE_FLAG" : false ,
"USE_CURRENT_PATH" : false
},
"PATH" : {
"INPUT_LOG_FILE_PATH" : "E:\\zwk\\Code\\logger_read\\data\\pro_data" ,
"SAVE_SPILT_MERGE_LOG_PATH" : "E:\\zwk\\Code\\logger_read\\output\\spilt_merge_log" ,
"OUTPUT_RESULT_PATH" : "E:\\zwk\\Code\\logger_read\\output"
},
"PARAMETERS" : {
"windows_size" : 2 ,
"duplicate_times" : 1
}
}
再对参数进行解析,
import json
def main():
# outfile_path = os.path.normpath("output/filtered")
global parameters
if(config['FLAG']['USE_CURRENT_PATH']):
pwd = os.getcwd()
config['PATH']['INPUT_LOG_FILE_PATH'] = os.path.join(pwd, 'data')
config['PATH']['SAVE_SPILT_MERGE_LOG_PATH'] = os.path.join(pwd, 'output')
config['PATH']['OUTPUT_RESULT_PATH'] = os.getcwd()
output_file_path = config['PATH']
flag = config['FLAG']
if __name__ == '__main__':
this_folder = os.path.dirname(os.path.abspath(__file__))
config_file = os.path.join(this_folder, 'config.json')
exists_check = os.path.isfile(config_file)
if not exists_check:
print('Error: loss of config file, Exit !!!')
with open(config_file, 'r') as f:
config = json.load(f)
# improve it, use as global variable
parameters = config['PARAMETERS']
main()
目录、文件名
在这里,示例我认为的还不错做法,主要是不受系统影响
this_folder = os.path.dirname(os.path.abspath(__file__))
config_file = os.path.join(this_folder, 'config.json')
exists_check = os.path.isfile(config_file)
if not exists_check:
print('Error: loss of config file, Exit !!!')
with open(config_file, 'r') as f:
config = json.load(f)
# 省略中间
# write output to files
os.makedirs(outfile['SAVE_SPILT_MERGE_LOG_PATH'], exist_ok=True)
记录一次利用 python 进行日志模块开发过程的更多相关文章
- 利用python 下paramiko模块无密码登录
利用python 下paramiko模块无密码登录 上次我个大家介绍了利用paramiko这个模块,可以模拟ssh登陆远程服务器,并且可以返回执行的命令结果,这次给大家介绍下如何利用已经建立的密钥 ...
- python 利用python的subprocess模块执行外部命令,获取返回值
有时执行dos命令需要保存返回值 需要导入库subprocess import subprocess p = subprocess.Popen('ping www.baidu.com', shell= ...
- python标准日志模块logging及日志系统设计
最近写一个爬虫系统,需要用到python的日志记录模块,于是便学习了一下. python的标准库里的日志系统从Python2.3开始支持.只要import logging这个模块即可使用.如果你想开发 ...
- python的日志模块:logging;django的日志系统;django日志输出时间修改
Django的log,主要是复用Python标准库中的logging模块,在settings.py中进行配置 源代码 1.__init__.py包含以下类: StreamHandler Formatt ...
- python标准日志模块logging的使用方法
参考地址 最近写一个爬虫系统,需要用到python的日志记录模块,于是便学习了一下.python的标准库里的日志系统从Python2.3开始支持.只要import logging这个模块即可使用.如果 ...
- Python logging(日志)模块
python日志模块 内容简介 1.日志相关概念 2.logging模块简介 3.logging模块函数使用 4.logging模块日志流处理流程 5.logging模块组件使用 6.logging配 ...
- python 工业日志模块 未来的python日志最佳实践
目录 介绍 好的功能 安装方法 参数介绍 呆log 参数与 使用方法 版本说明 后期版本规划 todo 感谢 介绍 呆log:工业中,python日志模块,安装即用.理论上支持 python2, py ...
- python logging日志模块
一.logging模块的简介 logging模块是Python内置的标准模块,主要用于输出运行日志,可以设置输出日志的等级.日志保存路径.日志文件回滚等:相比print,具备如下优点: 可以通过设置不 ...
- python的日志模块logging和syslog
syslog模块是在unix环境下工作的模块,不能用于windows,在windows环境下可以使用logging模块. 一.syslog syslog模块可以用于记录系统运行的信息,这个模块提供的方 ...
随机推荐
- quota磁盘配额
一.什么是磁盘配额 磁盘配额从字面意思上看就是给一个磁盘配置多少额度,而quota就是有多少限额的意思,所以总的来说就是限制用户对磁盘空间的使用量.因为Linux是多用户多任务的操作系统,许多人公用磁 ...
- jackson 学习资料
源代码托管地址 https://github.com/FasterXML/jackson https://github.com/FasterXML/jackson-docs http://www.st ...
- mongoDB看这篇就够了
写在前面 hello,小伙伴们,我是 pubdreamcc ,本篇文章依旧出至于我的 GitHub仓库 node学习教程 ,如果你觉得我写的还不错,欢迎给个 star ,小伙伴们的 star 是我持续 ...
- redis-启用命令
一.redis后端启动: 1.将redis源码包中的redis.conf配置文件复制到redis/bin/下 # cd /root/redis-3.0.0 # cp redis.conf /usr/l ...
- tensorflow 镜像
https://mirrors.tuna.tsinghua.edu.cn/tensorflow/windows/cpu/ 报错 不支持 C:\Users\brady\.conda\envs\tenso ...
- 【Python】列表推导式
1. 列表推导式 list1 = [1, 3, 5, 6, 8] list2 = [x * 2 for x in list1] print(list2) # [2, 6, 10, 12, 16]
- linux防火墙和xshell的链接
centos7用的firewalld 1.firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: ...
- 【Maven】Maven中排除依赖、归类依赖、优化依赖
参考博文:Maven中排除依赖.归类依赖.优化依赖
- 记录一次win2003服务器的IIS服务加载.flv后缀的资源报错404的处理方法
问题:访问某个域名下的xxxx.flv资源,页面报错404. 解决思路: 1.权限是否给足 user权限给完全控制咯 如果你访问该域名下的其他资源无问题的话就不是介个原因了 2.MIME类型是否少了 ...
- Httpd服务入门知识-Httpd服务常见配置案例之日志设定
Httpd服务入门知识-Httpd服务常见配置案例之日志设定 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.日志类型 [root@node101.yinzhengjie.org ...