python+flask 分分钟完美解析阿里云日志

 

             拿到了自己阿里云服务器的日志,对其需要进行处理。

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Read_Rizhi:
    def __init__(self,filename):
        self.filename=filename
    def open_file(self):
        try:
            f = open(self.filename, 'r', encoding='utf-8')
            resuly = {'code': 1, 'result': f}
        except Exception as e:
            resuly = {'code': 0, 'result': e}
        return resuly
    def read_line(self):
        result=self.open_file()
        if result['code']==0:
            return  {'read':'fail','relust':result['result']}
        elif result['code']==1:
            return {'read':'pass','relust':result['result'].readlines()}
        else:
            return {'read':'error','relust':'未知错误'}
    def print_eachline(self,splist:str):
        eachline=self.read_line()
        if eachline['read']=='pass':
            for rizhi in eachline['relust']:
                ri=rizhi.split(splist)
                print('请求ip:', ri[0])
                print('请求时间磋:', ri[3])
                print('请求方式:', ri[5])
                print('请求路径:', ri[6])
                print('请求协议:', ri[7])
                print('返回状态吗:', ri[8])
        elif eachline['read']=='fail':
            print('读取失败!原因:%s'%eachline['relust'])
        else:
            print('读取异常')
if __name__=='__main__':
    rizhi=Read_Rizhi('access.log')
    rizhi.print_eachline(' ')

  对日志解析进行封装,对日志的需求进行了自己的分析,

  

 学了flask,你能不能吧这个日志给我放到flask 给一个前端的界面去展示呢,答案是没有问题的,对代码进行修改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Read_Rizhi:
    def __init__(self,filename):
        self.filename=filename
    def open_file(self):
        try:
            f = open(self.filename, 'r', encoding='utf-8')
            resuly = {'code': 1, 'result': f}
        except Exception as e:
            resuly = {'code': 0, 'result': e}
        return resuly
    def read_line(self):
        result=self.open_file()
        if result['code']==0:
            return  {'read':'fail','relust':result['result']}
        elif result['code']==1:
            return {'read':'pass','relust':result['result'].readlines()}
        else:
            return {'read':'error','relust':'未知错误'}
    def print_eachline(self,splist:str):
        eachline=self.read_line()
        if eachline['read']=='pass':
            ip_list=[]
            for rizhi in eachline['relust']:
                ri=rizhi.split(splist)
                ip_list.append({'ip':ri[0],'time':ri[3],
                                'meth':ri[5],'path':ri[6],'xieyi':ri[7],
                                'code':ri[8]})
            relust={'code':1,'result':ip_list}
        elif eachline['read']=='fail':
            relust = {'code':2, 'result':eachline['relust']}
        else:
            relust = {'code': 3, 'result':'读取异常'}
        return relust

 flask部分代码如下:

复制代码
from flask import Flask,render_template
from jiexi import Read_Rizhi
app = Flask(__name__)
@app.route('/')
def hello_world():
    rizhi = Read_Rizhi(r'C:\\Users\Administrator\Desktop\\untitled1\access.log')
    relust = rizhi.print_eachline(' ')
    if relust['code'] == 1:
        f_list = relust['result']
    return render_template('rizi.html',f_lis=f_list)
if __name__ == '__main__':
    app.run()
复制代码

rizi.html部分代码:

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>阿里云日志分析</title>
</head>
<body>
<h1 style="text-align: center">阿里云日志分析</h1>
<table style="width: 60%;margin-top: 40px" border="1">
    <tbody>
    <tr>
        <td>ip</td>
        <td>时间</td>
        <td>请求方式</td>
        <td>请求路径</td>
        <td>协议</td>
        <td>状态码</td>
    </tr>
    {% for item in f_lis%}
    <tr>
    <td>{{ item.ip }}</td>
    <td>{{ item.time }}</td>
    <td>{{ item.meth }}</td>
    <td>{{ item.path }}</td>
    <td>{{ item.xieyi }}</td>
    <td>{{ item.code }}</td>
    </tr>
    {% endfor %}
    </tbody>
</table>
</body>
</html>
复制代码

启动flask模块,

访问:

 

 这样我们进一步优化就结束了,其实还可以进行优化,

这样还得需要我们进一步去的优化,部分切割还是不完善的。简单的切割,展示完成。十分钟就能实现的一个小功能。

posted @   北漂的雷子  阅读(891)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示