0

我有一个文件(application.log),我的应用程序在其中存储它们的日志。有时我有一个异常,我只想将这些异常存储在另一个文件(exception.log)中。每行日志都从日期开始,格式如下:

[2017-28-09 10:00:00,000] Text of log number 1
[2017-28-09 10:00:05,000] Text of log number 2
[2017-28-09 10:00:10,000] Text of Exception number 1
 at bla bla bla
 at bla bla bla    
 at bla bla bla    
 at bla bla bla    
[2017-28-09 10:00:15,000] Text of log number 4

在这种情况下,应存储 exception.log:

[2017-28-09 10:00:10,000] Text of Exception number 1
 at bla bla bla
 at bla bla bla    
 at bla bla bla    
 at bla bla bla  

我试过这样的方法:

kill $(ps -ef | grep tail | awk '{print $2}')
tail -f /path/to/my/application.log | pcregrep -M 'Exception[^\[]+' | while read line
do
(echo "$line" >> /path/to/my/exception.log)
done &

该解决方案成功地完成了这项工作,但它也创建了许多线程,并且我系统中的总线程数急剧增加。因此,我需要使用另一种解决方案或解决“已失效的问题”。

伙计们,你们知道如何将异常堆栈跟踪剪切、grep 或复制到另一个文件或防止不打开失效的线程吗?

4

3 回答 3

2
$ cat test.txt 
[2017-28-09 10:00:00,000] Text of log number 1
[2017-28-09 10:00:05,000] Text of log number 2
[2017-28-09 10:00:10,000] Text of Exception number 1
 at bla bla bla
 at bla bla bla    
 at bla bla bla    
 at bla bla bla    
[2017-28-09 10:00:15,000] Text of log number 4

$ awk '/\[.*\]/{d=0; if($0 ~ "Exception")d=1}d' test.txt
[2017-28-09 10:00:10,000] Text of Exception number 1
 at bla bla bla
 at bla bla bla    
 at bla bla bla    
 at bla bla bla
于 2017-09-28T13:25:22.600 回答
0

下面是@CWLiu 解决方案和一种没有“tail -f”的解决方案。简单但可用,并且没有僵尸进程或 tail -f 线程挂起:

#!/bin/bash

# Below I checked if last.log file exists
if [ ! -f /path/to/my/last.log ]; then
   echo "" > /path/to/my/last.log
fi

# Thanks @CWLiu again for your below solution
awk '/\[.*\]/{d=0; if($0 ~ "Exception")d=1}d' /path/to/my/application.log > /path/to/my/now.log

# Creation differential file - now.log stores all exceptions, last.log stores all exceptions from previous check
diff /path/to/my/now.log /path/to/my/last.log > /path/to/my/tmp.log
diff /path/to/my/last.log /path/to/my/now.log | grep ">" | tr -d '>' >> /path/to/my/exception.log

现在我可以在 crontab 中添加这个文件的执行来检查 ie 是否每分钟发生一些异常:

* * * * * /path/to/my/file.sh
于 2017-09-28T14:35:11.403 回答
0

告诉 grep 在匹配以空格开头的行之前打印一行。

grep -B1 '^ '
于 2017-09-28T11:43:29.887 回答