internetitem / logback-elasticsearch-appender

Logback Elasticsearch Appender
Other
233 stars 137 forks source link

Setting properties with logback.groovy #46

Closed jmorrispa closed 6 years ago

jmorrispa commented 6 years ago

I'm trying to set AbstractElasticsearchAppender.elasticsearchProperties from logback.groovy but the method AbstractElasticsearchAppender.setProperties(...) is never called.

Appender configuration is:

appender("ELASTIC", ElasticsearchAppender){ it ->
    ElasticsearchProperties e = new ElasticsearchProperties()
    e.addProperty( new Property("logger", '%logger', false))
    properties = e
    url = 'http://localhost:9200/_bulk'
    index = 'aps-logs-%date{yyyy-MM-dd}'
    type = 'log'
    rawJsonMessage = false
    errorsToStderr = true
    includeCallerData = true
    def configHeaders = new HttpRequestHeaders()
    configHeaders.addHeader(new HttpRequestHeader(name: 'Content-Type', value: 'application/x-ndjson'))
    headers = configHeaders
}

I can see logs sent to elasticsearch, but the logger property is not there.

Using logback.xml configuration works fine.

I tried using a property name other that "properties" like this:

    AbstractElastucsearchAppender.java
    ....
    public void setProperties(ElasticsearchProperties elasticsearchProperties) {
        this.elasticsearchProperties = elasticsearchProperties;
    }

     public void setProps(ElasticsearchProperties elasticsearchProperties) {
        this.elasticsearchProperties = elasticsearchProperties;
    }

With that code and using props instead of properties it works :(

I attached a sample with both groovy and xml logback configurations where you can see that setting "properties" from logback.groovy is not working.

@balopat I will really appreciate if you can replicate and confirm this issue since this is a bit weird.

sample.zip

jmorrispa commented 6 years ago

I found an answer. In order to access properties field in ElasticsearchAppender using logback.groovy use component.properties, Ex:

appender("ELASTIC", ElasticsearchAppender){
    ElasticsearchProperties e = new ElasticsearchProperties()
    e.addProperty( new Property("logger", '%logger', false ))
    e.addProperty( new Property("host", System.getenv("HOSTNAME"), false ))
    component.properties = e
    url = 'http://localhost:9200/_bulk'
    index = 'app-logs-%date{yyyy-MM-dd}'
    type = 'log'
    rawJsonMessage = false
    errorsToStderr = true
    includeCallerData = true
    def configHeaders = new HttpRequestHeaders()
    configHeaders.addHeader(new HttpRequestHeader(name: 'Content-Type', value: 'application/x-ndjson'))
    headers = configHeaders
}