6

I'm using Logback and Logstash in a SpringBoot application.

In the logback.xml I have a property with the name of the service, and is like:

<configuration>

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

<include resource="org/springframework/boot/logging/logback/console-appender.xml" />

<property name="spring.application.name" calue="service"/>

<appender name="stash" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
    <destination>localhost:9600</destination>
    <encoder class="net.logstash.logback.encoder.LogstashEncoder"/>
</appender>

<root level="INFO">
    <appender-ref ref="CONSOLE" />
    <appender-ref ref="stash" />
</root>

</configuration>

The Logstash conf file is like:

input{ tcp{
        port=> 9600
        host=>logstash
    }
}

filter {
grok {
match => {
  "message" =>
  "^%{TIMESTAMP_ISO8601:timestamp}\s+%{LOGLEVEL:level}\s+%{NUMBER:pid}\s+---\s+\[\s*%{USERNAME:thread}\s*\]\s+%{JAVAFILE:class}\s*:\s*%{DATA:themessage}(?:\n+(?<stacktrace>(?:.|\r|\n)+))?$"
}
}
date {
match => [ "timestamp" , "yyyy-MM-dd HH:mm:ss.SSS" ]
}
 mutate {
remove_field => ["@version"]
add_field => {
  "appid" => "%{[path]}"
}
add_field => {
  "levell" => "level"
}
add_field => {
  "mensage" => "message"
}
 }
   }
output{

 elasticsearch {
hosts => ["elasticsearch"]
index => "indice"
 }
   stdout{}
 } 

How can I do to add the property of application name from the logback file as a field?

AleGallagher
  • 1,745
  • 7
  • 30
  • 40

2 Answers2

13

You may configure the custom fields for LogstashEncoder as follows

<appender name="stash" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
    <destination>192.168.99.100:4560</destination>

    <!-- encoder is required -->
    <encoder class="net.logstash.logback.encoder.LogstashEncoder">
      <customFields>{"appname":"${appName}"}</customFields>
    </encoder>
</appender>

For example, for spring boot application you can use get the spring scope properties as follows

<springProperty name="appName" source="spring.application.name"/>

Or otherwise import properties from .properties file

<property  resource="application.properties" />
Mickael Tardy
  • 311
  • 3
  • 6
  • Does application.yml file work with this config? I am trying `` but it is not working. – Nikhil Kakade Dec 28 '19 at 07:18
  • @nikhil-kakade To use a YAML config : ``, please take a look at : [response of @granadacoder](https://stackoverflow.com/a/54288986/2971820) – jpmottin May 11 '21 at 16:13
2

From the logstash-logback-encoder docs:

By default, each property of Logback's Context (ch.qos.logback.core.Context), such as HOSTNAME, will appear as a field in the LoggingEvent. This can be disabled by specifying false in the encoder/layout/appender configuration.

By default your logback properties are local scope and aren't included. Try setting them to scope="context".

<property name="spring.application.name" value="service" scope="context"/>
roby
  • 3,103
  • 1
  • 16
  • 14
  • Thanks @roby ! The property appears in the log, but whitin "message" field. I want to have the property as field, to filter the logs with Kibana. Do you know how to do that? I tried to put this in the logastash conf file, but it didn't work: `add_field => { "service" => ["@spring.application.name"] }` – AleGallagher Dec 25 '16 at 00:01