如何解决分析日志文件时如何获得所有行的输出?
python loganalyzer.py [filepath_to_logfile] [action]
指定的操作决定了脚本输出的内容。 当输入 error 或 notice 作为操作时,输出为:
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 举报,一经查实,本站将立刻删除。