2016-08-19 78 views
0

我正在使用LogStash接收来自日志文件的数据,日志文件具有不同类型的日志。如何在Logstash配置文件中包含过滤器?

第一行表示自定义日志,而第二行表示JSON格式的日志。

现在,我想编写一个过滤器,它将根据内容解析日志,并最终将所有JSON格式的日志导向一个名为jsonformat.log的文件,而另一个日志则记录到一个单独的文件中。

回答

2

您可以利用json过滤器并检查它是否失败或不确定发送事件的位置。

input { 
    file { 
     path => "/Users/mysystem/Desktop/abc.log" 
     start_position => beginning 
     ignore_older => 0 
    } 
} 
filter { 
    json { 
    source => "message" 
    } 
} 
output { 
    # this condition will be true if the log line is not valid JSON 
    if "_jsonparsefailure" in [tags] { 
    file { 
     path => "/Users/mysystem/Desktop/nonjson.log" 
    } 
    } 
    # this condition will be true if the log line is valid JSON 
    else { 
    file { 
     path => "/Users/mysystem/Desktop/jsonformat.log" 
    } 
    } 
} 
+0

我不知道为什么,但虽然你的代码将配置测试,当我在我的输入文件运行它,它只是表明,管道已经启动,但它不会创建日志文件。 – rohansingh

+0

您需要将新行添加到您的'abc.log'文件或将'sincedb_path =>“/ dev/null”'添加到您的文件输入中。 – Val

+0

是的,我做到了。我甚至尝试添加新行,但它仍然不会创建新文件。我张贴我的配置文件供你看看。还有,输入文件。 (在提问中作为编辑的一部分提到) – rohansingh