logstash-output-file-as 基于时间

logstash-output-file-as time based

我的 logstash 输出指向名为 apache.log 的文件。 这个文件需要每小时生成一次。

例如:apache-2018-04-16-10:00.log 或类似的内容。

这是我的配置文件:

# INPUT HERE
input {
    beats {
          port => 5044
    }
}

# FILTER HERE
filter {
    if [source]=="/var/log/apache2/error.log"
    {
        mutate {
            remove_tag => [ "beats_input_codec_plain_applied" ]
            add_tag => [ "apache_logs" ]
        }
    }
    if [source]=="/var/log/apache2/access.log"
    {
        mutate {
            remove_tag => [ "beats_input_codec_plain_applied" ]
            add_tag => [ "apache_logs" ]
        }
    }
}

# OUTPUT HERE
output {
    if "apache_logs" in [tags] {
        file {
            path => "/home/ubuntu/apache/apache-%{+yyyy-mm-dd}.log"
                codec => "json"
        }
    }
}

请大家帮忙解决

根据 joda-time 文档 (http://www.joda.org/joda-time/key_format.html),您有 H hour of day (0~23)。所以解决你的问题的输出配置是:

output {
    if "apache_logs" in [tags] {
        file {
            path => "/home/ubuntu/apache/apache-%{+yyyy-mm-dd-HH}.log"
                codec => "json"
        }
    }
}