分析日志文件时如何获得所有行的输出?

如何解决分析日志文件时如何获得所有行的输出?

在终端中键入以下内容时,脚本应该能够运行和分析日志文件

python loganalyzer.py [filepath_to_logfile] [action]

指定的操作决定了脚本输出内容。 当输入 errornotice 作为操作时,输出为:

date : message
date : message
date : message
date : message

但是脚本并没有输出所有的行...为什么?

例如,当要求通知时,我得到的输出是:

[Mon Dec 05 14:01:48 2005]  :  workerEnv.init() ok /etc/httpd/conf/workers2.properties

[Mon Dec 05 14:11:40 2005]  :  jk2_init() Found child 6115 in scoreboard slot 10

[Mon Dec 05 14:11:45 2005]  :  workerEnv.init() ok /etc/httpd/conf/workers2.properties

[Mon Dec 05 15:31:06 2005]  :  jk2_init() Found child 6260 in scoreboard slot 7

[Mon Dec 05 15:31:09 2005]  :  jk2_init() Found child 6261 in scoreboard slot 8

[Mon Dec 05 15:31:10 2005]  :  workerEnv.init() ok /etc/httpd/conf/workers2.properties

[Mon Dec 05 15:40:59 2005]  :  jk2_init() Found child 6276 in scoreboard slot 6

[Mon Dec 05 15:41:32 2005]  :  workerEnv.init() ok /etc/httpd/conf/workers2.properties

[Mon Dec 05 15:45:42 2005]  :  jk2_init() Found child 6285 in scoreboard slot 8

[Mon Dec 05 15:45:44 2005]  :  workerEnv.init() ok /etc/httpd/conf/workers2.properties

[Mon Dec 05 15:50:53 2005]  :  jk2_init() Found child 6294 in scoreboard slot 7

[Mon Dec 05 15:51:18 2005]  :  jk2_init() Found child 6296 in scoreboard slot 6

[Mon Dec 05 15:51:20 2005]  :  workerEnv.init() ok /etc/httpd/conf/workers2.properties

[Mon Dec 05 15:55:31 2005]  :  jk2_init() Found child 6302 in scoreboard slot 8

[Mon Dec 05 15:55:32 2005]  :  workerEnv.init() ok /etc/httpd/conf/workers2.properties

[Mon Dec 05 16:01:17 2005]  :  jk2_init() Found child 6310 in scoreboard slot 6

[Mon Dec 05 16:02:00 2005]  :  jk2_init() Found child 6316 in scoreboard slot 7

它只有 34 行中的 17 行。

这是我所有的代码

import argparse

error = {}
notice = {}
log_file = 'test.log'

#   Functions
def load():
    with open('test.log') as logfile:
        for line in logfile:
            parts = line.split('[error]')
            if len(parts) == 2:
                error[parts[0]] = parts[1]
            parts = line.split('[notice]')
            if len(parts) == 2:
                notice[parts[0]] = parts[1]

def errors():
    for date,info in error.items():
        print(date + ' : ' + info)

def notices():
    for date,info in notice.items():
        print(date + ' : ' + info)
        
def statistics():
    file = open('test.log','r')
    error_counter = 0-1
    content = file.read()
    errors = content.split('[error]')

    for error in errors:
        if error:
            error_counter += 1
    print('Errors: ' + str(error_counter))
    
    notice_counter = 0-1
    notices = content.split('[notice]')

    for notice in notices:
        if notice:
            notice_counter += 1
    print('Notices: ' + str(notice_counter))


#   Run
if __name__ == '__main__':
    load()
    notices()

#    parser = argparse.ArgumentParser()
#    parser.add_argument('logfile',help = 'Run program by typing python.exe logglooker.py [path_to_logfile] [action]')
#    parser.add_argument('notice',help = 'Use this action to output all the date and message of all notice entries.')
#    parser.add_argument('error',help = 'Use this action to output all the data and message of all error entries.')
#    parser.add_argument('statistics',help = 'Use this action to output how many errors and notices it is in the logfile.')
#    args = parser.parse_args()
#    log_file = args.logfile

我还没有弄清楚如何让 argparse 正常工作,这就是为什么代码看起来有点奇怪。

要分析的 test.log 就是这个。

[Mon Dec 05 14:01:48 2005] [notice] workerEnv.init() ok /etc/httpd/conf/workers2.properties
[Mon Dec 05 14:01:48 2005] [error] mod_jk child workerEnv in error state 7
[Mon Dec 05 14:01:48 2005] [notice] workerEnv.init() ok /etc/httpd/conf/workers2.properties
[Mon Dec 05 14:01:48 2005] [error] mod_jk child workerEnv in error state 9
[Mon Dec 05 14:01:48 2005] [notice] workerEnv.init() ok /etc/httpd/conf/workers2.properties
[Mon Dec 05 14:01:48 2005] [error] mod_jk child workerEnv in error state 8
[Mon Dec 05 14:11:40 2005] [notice] jk2_init() Found child 6115 in scoreboard slot 10
[Mon Dec 05 14:11:43 2005] [error] [client 141.154.18.244] Directory index forbidden by rule: /var/www/html/
[Mon Dec 05 14:11:45 2005] [notice] workerEnv.init() ok /etc/httpd/conf/workers2.properties
[Mon Dec 05 14:11:45 2005] [error] mod_jk child workerEnv in error state 7
[Mon Dec 05 15:31:06 2005] [notice] jk2_init() Found child 6259 in scoreboard slot 6
[Mon Dec 05 15:31:06 2005] [notice] jk2_init() Found child 6260 in scoreboard slot 7
[Mon Dec 05 15:31:09 2005] [notice] jk2_init() Found child 6261 in scoreboard slot 8
[Mon Dec 05 15:31:10 2005] [notice] workerEnv.init() ok /etc/httpd/conf/workers2.properties
[Mon Dec 05 15:31:10 2005] [error] mod_jk child workerEnv in error state 6
[Mon Dec 05 15:31:10 2005] [notice] workerEnv.init() ok /etc/httpd/conf/workers2.properties
[Mon Dec 05 15:31:10 2005] [error] mod_jk child workerEnv in error state 6
[Mon Dec 05 15:31:10 2005] [notice] workerEnv.init() ok /etc/httpd/conf/workers2.properties
[Mon Dec 05 15:31:10 2005] [error] mod_jk child workerEnv in error state 6
[Mon Dec 05 15:40:59 2005] [notice] jk2_init() Found child 6277 in scoreboard slot 7
[Mon Dec 05 15:40:59 2005] [notice] jk2_init() Found child 6276 in scoreboard slot 6
[Mon Dec 05 15:41:32 2005] [notice] jk2_init() Found child 6280 in scoreboard slot 7
[Mon Dec 05 15:41:32 2005] [notice] jk2_init() Found child 6278 in scoreboard slot 8
[Mon Dec 05 15:41:32 2005] [notice] jk2_init() Found child 6279 in scoreboard slot 6
[Mon Dec 05 15:41:32 2005] [notice] workerEnv.init() ok /etc/httpd/conf/workers2.properties
[Mon Dec 05 15:41:32 2005] [error] mod_jk child workerEnv in error state 7
[Mon Dec 05 15:41:32 2005] [notice] workerEnv.init() ok /etc/httpd/conf/workers2.properties
[Mon Dec 05 15:41:32 2005] [error] mod_jk child workerEnv in error state 6
[Mon Dec 05 15:41:32 2005] [notice] workerEnv.init() ok /etc/httpd/conf/workers2.properties
[Mon Dec 05 15:41:32 2005] [error] mod_jk child workerEnv in error state 7
[Mon Dec 05 15:45:42 2005] [notice] jk2_init() Found child 6285 in scoreboard slot 8
[Mon Dec 05 15:45:44 2005] [notice] workerEnv.init() ok /etc/httpd/conf/workers2.properties
[Mon Dec 05 15:45:44 2005] [error] mod_jk child workerEnv in error state 6
[Mon Dec 05 15:50:53 2005] [notice] jk2_init() Found child 6293 in scoreboard slot 6
[Mon Dec 05 15:50:53 2005] [notice] jk2_init() Found child 6294 in scoreboard slot 7
[Mon Dec 05 15:51:18 2005] [notice] jk2_init() Found child 6297 in scoreboard slot 7
[Mon Dec 05 15:51:18 2005] [notice] jk2_init() Found child 6295 in scoreboard slot 8
[Mon Dec 05 15:51:18 2005] [notice] jk2_init() Found child 6296 in scoreboard slot 6
[Mon Dec 05 15:51:20 2005] [notice] workerEnv.init() ok /etc/httpd/conf/workers2.properties
[Mon Dec 05 15:51:20 2005] [error] mod_jk child workerEnv in error state 7
[Mon Dec 05 15:51:20 2005] [notice] workerEnv.init() ok /etc/httpd/conf/workers2.properties
[Mon Dec 05 15:51:20 2005] [error] mod_jk child workerEnv in error state 7
[Mon Dec 05 15:51:20 2005] [notice] workerEnv.init() ok /etc/httpd/conf/workers2.properties
[Mon Dec 05 15:51:20 2005] [error] mod_jk child workerEnv in error state 6
[Mon Dec 05 15:55:31 2005] [notice] jk2_init() Found child 6302 in scoreboard slot 8
[Mon Dec 05 15:55:32 2005] [notice] workerEnv.init() ok /etc/httpd/conf/workers2.properties
[Mon Dec 05 15:55:32 2005] [error] mod_jk child workerEnv in error state 6
[Mon Dec 05 16:01:17 2005] [notice] jk2_init() Found child 6310 in scoreboard slot 6
[Mon Dec 05 16:02:00 2005] [notice] jk2_init() Found child 6315 in scoreboard slot 6
[Mon Dec 05 16:02:00 2005] [notice] jk2_init() Found child 6316 in scoreboard slot 7

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?