记录一次利用 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模块可以用于记录系统运行的信息,这个模块提供的方 ...
随机推荐
- linux学习-防火墙指令
Redhat7之前的版本(iptables) 开启关闭防火墙 放行端口 RedHat7防火墙相关的指令(firewall-cmd) 安装firewall 本文内容适用于 redhat 和 centos ...
- .Net Core 程序集管理说明(加载)
.NET CORE 的程序集加载管理和以前的 .NET 发生了很大的变化, 在 .NET CORE 里, 程序集的加载, 依赖了 xx.deps.json 文件, deps.json 文件里,定义了程 ...
- IOS/Safari下document对象的scrollHeight值比Chrome更大
之前写滚动加载更多需求时,写了这样一段错误代码 应该获取滚动列表的scrollHeight.clientHeight和scrollTop而不是整个页面的. 因为整个页面不仅包括了滚动列表,还包括了头部 ...
- sendMessage 与 obtainMessage (sendToTarget)比较
我们平时在做到多线程问题的时候可能利用Handler去传递Message,其中,经常使用的就是 1.new Handler().obtainMessage().sendToTarget(); 2.ne ...
- Sublime Text3 安装 CTags 插件出现乱码
1.下载ctags.exe 可以直接下载我上传好的资源:http://download.csdn.net/download/zhaoxd200808501/9971251.或者网络上其他地方也可以下载 ...
- 【Docker】Docker容器中安装vim命令
1)先执行 apt-get update 2) 再执行 apt-get install vim
- GeoGebra 代数输入的提示和技巧
GeoGebra是Mac平台上一款自由且跨平台的动态数学软件,提供各级教育使用,包含了几何.代数.表格.图形.统计和微积分,集中在一个容易使用的软件. 确认表达式 始终按 Enter 键确认您 ...
- jenkiins 配置邮件发送(四)
一.默认邮件配置的方法 Jenkins默认的插件只能发送执行失败的job 系统管理-->系统设置-->邮件通知 需要注意的是系统管理员邮箱地址必须要邮箱通知的邮箱一致,否则不会发送成功 在 ...
- 数据结构中的堆(Heap)
堆排序总结 这是排序,不是查找!!!查找去找二叉排序树等. 满二叉树一定是完全二叉树,但完全二叉树不一定是满二叉树. 构建顶堆: a.构造初始堆 b.从最后一层非叶节点开始调整,一直到根节点 c.如果 ...
- 第七篇 -- XmlReader 和 XmlWriter
XmlReader用于读取Xml文件,XmlWriter用于将数据写到Xml文件.其实,在印象当中,XML很多的操作类都支持直接Save.Read也支持接受XmlReader与XmlWriter类的示 ...