Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将错误记录到文件中_Python - Fatal编程技术网

Python 将错误记录到文件中

Python 将错误记录到文件中,python,Python,我在其中一台服务器上有一个日志文件,我需要编写一个python脚本来复制包含错误和信息标记的行。我希望每次运行脚本时,所有带有标记的行都写入一个新文件 这是我到目前为止写的(我是个新手) trace1.txt示例: 03/17/2015 13:41:55|WARN||ajp-bio-127.0.0.1-4090-exec-5|495233559||_ERR: execute(): Error Response returned 03/17/2015 13:41:47|INFO||ajp-bio

我在其中一台服务器上有一个日志文件,我需要编写一个python脚本来复制包含错误和信息标记的行。我希望每次运行脚本时,所有带有标记的行都写入一个新文件

这是我到目前为止写的(我是个新手)

trace1.txt示例:

03/17/2015 13:41:55|WARN||ajp-bio-127.0.0.1-4090-exec-5|495233559||_ERR: execute(): Error Response returned 
03/17/2015 13:41:47|INFO||ajp-bio-127.0.0.1-4090-exec-2|495785359||_IN:SearchQuery getList(): ||||
试试这个:

my_errors = open("/var/tmp/myerrors1.txt", "w")
my_trace = open("/var/tmp/logs/trace1.txt", "r")

levels = ['INFO', 'WARN', 'ERROR']

for line in my_trace:
    if any(level in line for level in levels):
        my_errors.write(line)

my_errors.close()
my_trace.close()
使用以下
trace1.txt
文件进行测试:

03/17/2015 13:41:55|WARN||ajp-bio-127.0.0.1-4090-exec-5|495233559||_ERR: execute(): Error Response returned 
Do not copy this line
03/17/2015 13:41:47|INFO||ajp-bio-127.0.0.1-4090-exec-2|495785359||_IN:SearchQuery getList(): ||||
Do not copy this line
myerrors1.txt文件的结果:

03/17/2015 13:41:55|WARN||ajp-bio-127.0.0.1-4090-exec-5|495233559||_ERR: execute(): Error Response returned 
03/17/2015 13:41:47|INFO||ajp-bio-127.0.0.1-4090-exec-2|495785359||_IN:SearchQuery getList(): ||||
编辑2:

my_errors = open("/var/tmp/myerrors1.txt", "w")
my_trace = open("/var/tmp/logs/trace1.txt", "r")

levels = ['INFO', 'WARN', 'ERROR']

for line in my_trace:
    for level in levels:
        if level in line:
            my_errors.write(line)    

my_errors.close()
my_trace.close()

您尚未指定问题,但我认为您的问题是没有任何内容写入您的文件。您正在将
x
重新指定给其他对象(行
中的值)。您需要为其分配一个不同的变量,如下所示:

my_errors=open("/var/tmp/myerrors1.txt", "w")
my_trace=open("/var/tmp/logs/trace1.txt", "r")
my_trace.readline()

x = [INFO, WARN, ERRor]

for line in  my_trace:
    for message in line: # change from reassigning the x variable to something else
        for error in x: # iterating through the errors in x
            if error in my_trace: # checking if an error is in my_trace
                my_errors.writelines(error) # writing the error

my_errors.close()
my_trace.close()

当然,如果没有完整的信息,这不是最好的答案,但它应该是您可以用来修复代码的基础。

您的问题是什么?谢谢!谢谢你的答复。对不起,我的问题不清楚。如果一行包含上述任何日志级别(信息、警告或错误),我想将这些行从跟踪文件复制到另一行。感谢您的回复。我将尝试您的解决方案并进行更新。我尝试了上述解决方案,但我看到的问题是整个trace1.txt正在复制到myerrors1.txt,不要复制包含级别列表中项目的行。能否提供
trace1.txt
文件的片段?03/17/2015 13:41:55 | WARN | | ajp-bio-127.0.0.1-4090-exec-5 | 495233559 | | | u ERR:错误响应于2015年3月17日13:41:47 | INFO | | ajp-bio-127.0.0.1-4090-exec-2 | 495785359 | | | | | | | | | | | | | | | | | |我没有收到相同的问题。你能试着用我在解决方案中使用的
trace1.txt
的内容来测试它吗?
my_errors=open("/var/tmp/myerrors1.txt", "w")
my_trace=open("/var/tmp/logs/trace1.txt", "r")
my_trace.readline()

x = [INFO, WARN, ERRor]

for line in  my_trace:
    for message in line: # change from reassigning the x variable to something else
        for error in x: # iterating through the errors in x
            if error in my_trace: # checking if an error is in my_trace
                my_errors.writelines(error) # writing the error

my_errors.close()
my_trace.close()