Skip to content

Commit a443903

Browse files
authored
Simplify Spring Boot configuration (#65)
1 parent 672b17e commit a443903

File tree

3 files changed

+58
-33
lines changed

3 files changed

+58
-33
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,17 @@ We recommend using this library to log into a JSON log file and let Filebeat sen
9494

9595
## Getting Started
9696

97-
### Logging configuration
97+
### Step 1: Configure application logging
9898

99-
- [Logback](logback-ecs-encoder/README.md)
99+
- [Logback](logback-ecs-encoder/README.md) (default for Spring Boot)
100100
- [Log4j2](log4j2-ecs-layout/README.md)
101101
- [Log4j](log4j-ecs-layout/README.md)
102102

103-
### Filebeat configuration
103+
### Step 2: Enable APM log correlation (optional)
104+
If you are using the Elastic APM Java agent,
105+
set [`enable_log_correlation`](https://www.elastic.co/guide/en/apm/agent/java/current/config-logging.html#config-enable-log-correlation) to `true`.
106+
107+
### Step 3: Filebeat configuration
104108

105109
#### With `filebeat.yml` configuration file
106110

logback-ecs-encoder/README.md

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,47 +15,45 @@ Add a dependency to your application
1515

1616
## Step 2: use the `EcsEncoder`
1717

18-
All you have to do is to use the `co.elastic.logging.logback.EcsEncoder` instead of the default pattern encoder
19-
```xml
20-
<encoder class="co.elastic.logging.logback.EcsEncoder">
21-
<serviceName>my-application</serviceName>
22-
</encoder>
23-
```
18+
## Spring Boot applications
2419

25-
## Example `logback.xml` for Spring Boot applications
26-
20+
`src/main/resources/logback-spring.xml`
2721
```xml
2822
<?xml version="1.0" encoding="UTF-8"?>
2923
<configuration>
24+
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>
3025
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
31-
<include resource="org/springframework/boot/logging/logback/file-appender.xml"/>
32-
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
33-
<encoder class="co.elastic.logging.logback.EcsEncoder">
34-
<serviceName>my-application</serviceName>
35-
</encoder>
36-
</appender>
37-
<appender name="json-file" class="ch.qos.logback.core.rolling.RollingFileAppender">
38-
<encoder class="co.elastic.logging.logback.EcsEncoder">
39-
<serviceName>my-application</serviceName>
40-
</encoder>
41-
<file>${LOG_FILE}.json</file>
42-
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
43-
<fileNamePattern>${LOG_FILE}.json.%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
44-
<maxFileSize>${LOG_FILE_MAX_SIZE:-10MB}</maxFileSize>
45-
<maxHistory>${LOG_FILE_MAX_HISTORY:-0}</maxHistory>
46-
</rollingPolicy>
47-
</appender>
48-
26+
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
27+
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
28+
<include resource="co/elastic/logging/logback/boot/ecs-file-appender.xml" />
4929
<root level="INFO">
50-
<appender-ref ref="console"/>
51-
<appender-ref ref="json-file"/>
52-
<!-- uncomment this if you also want to log in plain text
30+
<appender-ref ref="CONSOLE"/>
31+
<appender-ref ref="ECS_JSON_FILE"/>
5332
<appender-ref ref="FILE"/>
54-
-->
5533
</root>
5634
</configuration>
5735
```
5836

37+
You also need to configure the following properties to your `application.properties`:
38+
39+
```properties
40+
spring.application.name=my-application
41+
# for Spring Boot 2.2.x+
42+
logging.file.name=/path/to/my-application.log
43+
# for older Spring Boot versions
44+
logging.file=/path/to/my-application.log
45+
```
46+
47+
## Other applications
48+
49+
All you have to do is to use the `co.elastic.logging.logback.EcsEncoder` instead of the default pattern encoder in `logback.xml`
50+
51+
```xml
52+
<encoder class="co.elastic.logging.logback.EcsEncoder">
53+
<serviceName>my-application</serviceName>
54+
</encoder>
55+
```
56+
5957
## Encoder Parameters
6058

6159
|Parameter name |Type |Default|Description|
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
ECS JSON file appender logback configuration provided for import, similar to the file-appender.xml included in Spring Boot
5+
<include resource="co/elastic/logging/logback/boot/ecs-file-appender.xml" />
6+
-->
7+
8+
<included>
9+
<springProperty name="SERVICE_NAME" source="spring.application.name"/>
10+
<appender name="ECS_JSON_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
11+
<encoder class="co.elastic.logging.logback.EcsEncoder">
12+
<serviceName>${SERVICE_NAME:-spring-boot-application}</serviceName>
13+
</encoder>
14+
<file>${LOG_FILE}.json</file>
15+
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
16+
<cleanHistoryOnStart>${LOG_FILE_CLEAN_HISTORY_ON_START:-false}</cleanHistoryOnStart>
17+
<fileNamePattern>${ROLLING_FILE_NAME_PATTERN:-${LOG_FILE}.json.%d{yyyy-MM-dd}.%i.gz}</fileNamePattern>
18+
<maxFileSize>${LOG_FILE_MAX_SIZE:-10MB}</maxFileSize>
19+
<maxHistory>${LOG_FILE_MAX_HISTORY:-7}</maxHistory>
20+
<totalSizeCap>${LOG_FILE_TOTAL_SIZE_CAP:-0}</totalSizeCap>
21+
</rollingPolicy>
22+
</appender>
23+
</included>

0 commit comments

Comments
 (0)