记录一次利用 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模块可以用于记录系统运行的信息,这个模块提供的方 ...
随机推荐
- PHP-FPM的相关知识的深度解释
一.需要搞清楚几个名词概念 1. CGI(Common Gateway Interface,CGI)通用网关接口, 是Web 服务器运行时外部程序的规范,按CGI 编写的程序可以扩展 ...
- 2019 美团java面试笔试题 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.美团等公司offer,岗位是Java后端开发,因为发展原因最终选择去了美团,入职一年时间了,也成为了面试官,之 ...
- Mac安装Java的JDK并进行环境配置
一.下载JDK 1.直接进入oracle官网下载页: https://www.oracle.com/technetwork/java/javase/downloads/index.html 2.选择版 ...
- Java 面向对象(八) 权限修饰符 和 final、native 关键字
一.权限修饰符 1.概述 在 Java 中提供了四种访问权限,使用不同的访问权限修饰符修饰时,被修饰的内容会有不同的访问权限: public:公共的: protected:受保护的: default: ...
- SAP技术 - How to create a CDS redirect view for a given database table
Scenario Suppose we have a database table A, and then we create a CDS redirect view B for it, then e ...
- oracle-常用sql语句和函数
1.求字符串长度 --计算字符串长度的函数 select length('你好世界!') len from dual; 2.常用函数 -- dbms_random.value(1,7) 获取(1,7) ...
- 认识Redis
认识的Redis 官方原文: Redis is an open source (BSD licensed), in-memory data structure store, used as a dat ...
- Request和Response。
复习点:1.重定向问题 2.输出字符串到浏览器.3.文件下载需求:1. 页面显示超链接2. 点击超链接后弹出下载提示框3. 完成图片文件下载 Request和Response Request: 1. ...
- Mac原型动画设计软件Drama创建3D图层的注意事项,你知道吗?
Drama创建3D图层的注意事项:要跨层保留3D空间,可以使用组.它们具有保留子图层和嵌套组的3D空间的特殊功能. Drama支持通过在三维空间中定位和旋转2D图层来创建3D内容.要在3D空间中定位图 ...
- Beta冲刺第5次
二.Scrum部分 1. 各成员情况 翟仕佶 学号201731103226 今日进展 新增将图片转为粉笔画功能代码 存在问题 难者不会,会者不难,主要是参数设置问题 明日安排 视情况而定,可能还是写扩 ...