title: logstsh-if-conf.d
date: 2016-05-28 14:57:55

tags:logstash

一般情况下,logstash 测试和运行环境存在区别的。
为了方便,logstash 测试的时候会使用如下命令:

1
/opt/logstash/bin/logstash -f /opt/logstash/conf.d/test.conf

测试没有问题。然后就会使用如下方式:

1
service logstash restart

让logstash 程序读取conf.d 目录下所有conf文件,然后启动程序。
这样就会带来一个问题,conf.d的文件 会被加载一个logstash 进程运行,多个conf 文件没有相互隔离。会出现测试时候不存在的问题。
解决方法:

1 使用 测试方法,将多个conf文件分开运行,写到脚本中

1
2
/opt/logstash/bin/logstash -f /opt/logstash/conf.d/test1.conf
/opt/logstash/bin/logstash -f /opt/logstash/conf.d/test2.conf

2 在配置文件中增加tag并做好if判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
input
file {
type => "app-log"
path => [ "/data/*.log" ]
start_position => "beginning"
codec => multiline {
pattern => "^%{TIMESTAMP_ISO8601} "
negate => true
what => previous
}
}
file {
type => "app-access-log"
path => [ "/data/*.log" ]
}
}
output {
if ([type] in ["app-log", "app-access-log"]) {
}
}

logstash 在生产环境,一般是用service方式运行,或者使用supervisord来运行。

评论