python对日志的处理及图形显示

Posted reblue520

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python对日志的处理及图形显示相关的知识,希望对你有一定的参考价值。

1.处理日志的python程序

def read(log):
    with open(log, encoding="UTF-8") as f:
        yield from f


def make_js(log, name):
    res = {}
    for line in read(log):
        line = line.split(" ")
        if len(line) > 10:
            key = line[10]
            value = res.get(key, 0) + 1
            res[key] = value + 1
    # print(res)
    with open("{}.js".format(name), "w") as f:
        f.write("res_data={result}".format(result = res))

make_js("store.log", "store")

# 日志样式
tail -n 3 /data/www/logs/nginx_log/access/store.log
1.1.1.1 - - [07/Feb/2020:19:38:20 -0800] - - "GET /jslibs/select2/select2.full.min.js?201812 HTTP/1.1" 200 23673 "https://store.wondershare.com/checkout/myCart/?code_id=cae3046452b6d2eb7a88f31a3172d752&machine_no=eus&_ga=2.164934399.1772424582.1581129371-825859103.1580980114" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36" 0.000 
1.1.1.1 - - [07/Feb/2020:19:38:20 -0800] - - "GET /images/checkout/btn_checkout_en_us.gif HTTP/1.1" 200 2290 "https://store.wondershare.com/checkout/myCart/?code_id=cae3046452b6d2eb7a88f31a3172d752&machine_no=eus&_ga=2.164934399.1772424582.1581129371-825859103.1580980114" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36" 0.000 
1.1.1.1 - - [07/Feb/2020:19:38:21 -0800] - - "GET /images/cart/cart.gif HTTP/1.1" 200 86 "https://store.wondershare.com/checkout/myCart/?code_id=cae3046452b6d2eb7a88f31a3172d752&machine_no=eus&_ga=2.164934399.1772424582.1581129371-825859103.1580980114" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36" 0.000 


2.最终生成日志的store.js
res_data={302: 10, 200: 90}

3.日志展示的html
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>ECharts</title>
    <!-- 引入 echarts.js -->
    <script src="js/echarts.min.js"></script>
    <script src="store.js"></script>
</head>
<body>
    <!-- 为ECharts准备一个具备大小(宽高)的Dom -->
    <div id="main" style="width: 600px;height:400px;"></div>
    <script type="text/javascript">
        // 基于准备好的dom,初始化echarts实例
        var myChart = echarts.init(document.getElementById(main));

        // 指定图表的配置项和数据
        var series_data = [], legend_data = [];
        for (var key in res_data) {
            series_data.push(res_data[key]),
                legend_data.push(key);
        }
        var option = {
            title: {
                text: ECharts 入门示例
            },
            tooltip: {},
            legend: {
                data:[status]
            },
            xAxis: {
                data: legend_data
            },
            yAxis: {},
            series: [{
                name: status,
                type: bar,
                data: series_data
            }]
        };

        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
    </script>
</body>
</html>

 

以上是关于python对日志的处理及图形显示的主要内容,如果未能解决你的问题,请参考以下文章

python常用代码片段总结

使用底图作为Python GUI中的图形

用Python shelve将canvas图形图像保存为文件及从文件读出图形图像重新显示的方法

Python 自动化 - 浏览器chrome打开F12开发者工具自动Paused in debugger调试导致无法查看网站资源问题原因及解决方法,javascript反调试问题处理实例演示(代码片段

Docker图形化管理工具--Portainer简介及部署

Linux任务处理及日志查看常用命令