一、日志解析代码
def nth_replace(string, old, new, n=1, option='only nth'):
if option == 'only nth':
left_join = old
right_join = old
elif option == 'all left':
left_join = new
right_join = old
elif option == 'all right':
left_join = old
right_join = new
else:
print("option参数不对,请选择: 'only nth' (default), 'all left' 或 'all right'")
return None
groups = string.split(old)
nth_split = [left_join.join(groups[:n]), right_join.join(groups[n:])]
return new.join(nth_split)
def construct_params(presql,params):
paramList = params.split(',')
for i in range(len(paramList)):
if "String" in paramList[i]:
paramList[i] = paramList[i].split("(")[0].strip()
presql=nth_replace(presql, '?', "'{}'", n=1, option="only nth")
if "Timestamp" in paramList[i]:
paramList[i] = paramList[i].split("(")[0].strip()
presql = nth_replace(presql, '?', "'{}'", n=1, option="only nth")
if "Integer" in paramList[i]:
paramList[i] = paramList[i].split("(")[0].strip()
presql = nth_replace(presql, '?', "{}", n=1, option="only nth")
if "Long" in paramList[i]:
paramList[i] = paramList[i].split("(")[0].strip()
presql = nth_replace(presql, '?', "{}", n=1, option="only nth")
return presql,paramList
def extract_logstr(logstr):
try:
Preparing = logstr.split("\n")[0].split("==>")[1].split(":",1)[1].strip('|[TID:N/A]|')
Parameters = logstr.split("\n")[1].split("==>")[1].split(":",1)[1].strip('|[TID:N/A]|')
except:
return """输入日志格式不正确,正确的为:294186bf4cd94a48bf9a841c314be48f|==> *****
****==> Parameters: 1004(String), 20(Integer)"""
Preparing,ParamList = construct_params(Preparing,Parameters)
print(Preparing.format(*ParamList)+";")
return Preparing.format(*ParamList)+";"
if __name__ == '__main__':
str = """2023-03-23T16:27:57.435+0800|DEBUG|http-nio-8080-exec-17|com.dao.OnlineUserDAO.query_COUNT.? ?||==> Preparing: SELECT count(0) FROM sys_user t LEFT JOIN bas_user u ON u.user_name = t.user_name AND u.node_code = t.node_code WHERE t.online_flag = 1 AND t.node_code = ? AND t.warehouse_code = ?
2023-03-23T16:27:57.436+0800|DEBUG|http-nio-8080-exec-17|com.dao.OnlineUserDAO.queryPage_COUNT.? ?||==> Parameters: 1000(String), 755DCG(String)"""
extract_logstr(str)
二、wxpython制作客户端GUI工具
使用wxpython可视化编辑器,绘制页面控件布局,通过文本框接收输入,并传递给extract_logstr方法,完成日志参数拼装,拼装结果返回给GUI页面展示。
https://blog.csdn.net/weixin_39625098/article/details/110168682
wxpython安装
https://wxpython.org/Phoenix/snapshot-builds/ python3.7
python -m pip install wxpython
# -*- coding:utf-8 -*-
import wx
from testPrint import *
from qrcode_gen2 import *
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title=u'测试办公小工具', size=(1000, 721),name='frame',style=541072960)
self.启动窗口 = wx.Panel(self)
self.Centre()
self.单选框1 = wx.RadioButton(self.启动窗口, size=(90, 28), pos=(45, 24), name='radioButton', label='日志参数填充')
self.单选框1.SetValue(True)
self.单选框2 = wx.RadioButton(self.启动窗口, size=(97, 23), pos=(150, 25), name='radioButton', label='生成单个SN')
self.单选框3 = wx.RadioButton(self.启动窗口, size=(80, 24), pos=(249, 25), name='radioButton', label='批量生成SN')
self.单选框4 = wx.RadioButton(self.启动窗口, size=(91, 29), pos=(341, 22), name='radioButton', label='托盘号')
self.edit1 = wx.TextCtrl(self.启动窗口, size=(872, 234), pos=(50, 60), value='',
name='text', style=wx.TE_MULTILINE | wx.TE_BESTWRAP)
self.edit2 = wx.TextCtrl(self.启动窗口, size=(876, 277), pos=(49, 369), value='',
name='text', style=wx.TE_MULTILINE | wx.TE_BESTWRAP)
self.button1 = wx.Button(self.启动窗口, size=(59, 26), pos=(50, 319), label='生成', name='button')
self.button2 = wx.Button(self.启动窗口, size=(55, 26), pos=(121, 319), label='清除', name='button')
self.button3 = wx.Button(self.启动窗口, size=(55, 26), pos=(191, 319), label='校验', name='button')
class myApp(wx.App):
def OnInit(self):
self.frame = Frame()
self.icon = wx.Icon(name='D:\\image.ico', type=wx.BITMAP_TYPE_ICO)
self.frame.SetIcon(self.icon)
self.Bind(wx.EVT_BUTTON, self.OnButtonConverse, self.frame.button1)
self.Bind(wx.EVT_BUTTON, self.OnButtonClear, self.frame.button2)
self.frame.Show(True)
return True
def OnButtonConverse(self, event):
if self.frame.单选框1.GetValue() == True:
str = self.frame.edit1.GetValue()
aftertrans = extract_logstr(str)
self.frame.edit2.SetValue(aftertrans)
if self.frame.单选框2.GetValue() == True:
str = self.frame.edit1.GetValue()
self.frame.edit2.SetValue("")
try:
fm = int(str.split(',')[0])
to = int(str.split(',')[1])
batch_gen_single(fm, to)
except:
errormsg = "输入格式不正确,正确格式为:\n10000,10003"
self.frame.edit2.SetValue(errormsg)
if self.frame.单选框3.GetValue() == True:
str = self.frame.edit1.GetValue()
self.frame.edit2.SetValue("")
try:
fm = int(str.split(',')[0])
to = int(str.split(',')[1])
text = compose_gen(fm, to)
self.frame.edit2.SetValue(text)
except:
errormsg = "输入格式不正确,正确格式为:\n10000,10003"
self.frame.edit2.SetValue(errormsg)
if self.frame.单选框4.GetValue() == True:
str = self.frame.edit1.GetValue()
self.frame.edit2.SetValue("")
try:
num = int(str)
res = gen_topanhao(num)
self.frame.edit2.SetValue(res)
except:
num = 20
res = gen_topanhao(num)
self.frame.edit2.SetValue(res)
def OnButtonClear(self, event):
self.frame.edit1.SetValue("")
self.frame.edit2.SetValue("")
self.frame.单选框1.SetValue(True)
if __name__ == '__main__':
app = myApp()
app.MainLoop()
三、使用pyinstaller打包成exe工具
pyinstaller -w -i D:\\image.ico -F singleChoose.py -p D:\\Python\\Python37\\Lib\\site-packages