Logstash logback编码器,logstash转发器和logstash

时间:2015-08-31 16:44:57

标签: elasticsearch logstash logstash-forwarder logstash-logback-encoder

倾向于建议https://blog.codecentric.de/en/2014/10/log-management-spring-boot-applications-logstash-elastichsearch-kibana/我设置了logstash编码器+ logstash转发器,将所有东西都推送到我的logstash deamon,最后将所有内容编入ElasticSearch中。

这是我的配置:

logstash.xml     

<included>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>

    <property name="FILE_LOGSTASH" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}.json"/>
    <appender name="LOGSTASH"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
        <file>${FILE_LOGSTASH}</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${FILE_LOGSTASH}.%i</fileNamePattern>
        </rollingPolicy>
        <triggeringPolicy
            class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MB</MaxFileSize>
        </triggeringPolicy>
        <encoder class="net.logstash.logback.encoder.LogstashEncoder">
            <includeCallerInfo>true</includeCallerInfo>                
        </encoder>
    </appender>

    <root level="INFO">
        <appender-ref ref="LOGSTASH"/>
    </root>
</included>

logstash-forwarder.conf

{
    "network": {
        "servers": [
            "logstash:5043"
        ],
        "ssl certificate": "/etc/pki/tls/certs/logstash-forwarder/logstash-forwarder.crt",
        "ssl key": "/etc/pki/tls/private/logstash-forwarder/logstash-forwarder.key",
        "ssl ca": "/etc/pki/tls/certs/logstash-forwarder/logstash-forwarder.crt",
        "timeout": 15
    },
    "files": [
        {
            "paths": [
                "${ENV_SERVICE_LOG}/*.log.json"
            ],
            "fields": {
                "type": "${ENV_SERVICE_NAME}"
            }
        }
    ]
}

logstash.conf

input {
    lumberjack {
        port => 5043

        ssl_certificate => "/etc/pki/tls/certs/logstash-forwarder/logstash-forwarder.crt"
        ssl_key => "/etc/pki/tls/private/logstash-forwarder/logstash-forwarder.key"
    }
}

output {
    elasticsearch { host => "localhost" }
}

一切正常,日志将保存在ElasticSearch中。

此时我希望能够指定ElasticSearch要编入索引的其他字段,例如日志级别。搜索@message内容是否存在错误或警告并不是很有用。

我该怎么做?我应该更改哪个配置以使该级别在ElasticSearch中显示为索引字段?

2 个答案:

答案 0 :(得分:1)

您正在寻找的是一个logstash 过滤器,它将作为输入和输出节的对等体在您的索引器上使用。

有大量过滤器(请参阅the doc),但您可以使用grok{}将regexp应用于消息字段并提取日志级别。

您没有包含示例消息,但是,如果给出类似“foo 123 bar”的字符串,则此模式会将“123”提取到名为loglevel的整数字段中:

grok {
    match => ["message", "foo %{NUMBER:loglevel:int} bar"]
}

关于在网络上编写grok模式的信息量很大。试试this one

答案 1 :(得分:0)

logstash config file:
input {
  file {
    path =>  [ "/tmp/web.log" ]
  }
}
filter {
    grok {
        match => [ "%{TIMESTAMP_ISO8601:timestamp} %{LOGLEVEL:severity} %{GREEDYDATA:message}" ]
       }
    }
output {
    elasticsearch {
        host => "127.0.0.1"
        index => "web-%{+YYYY.MM.dd}"
    }
}

您可以使用'add_tag'或'add_field'来指定特殊字段