Skip to content

Commit 893d2e5

Browse files
author
Mikalai Alimenkou
committed
Added HTTP logging with Logbook for all logging profiles
1 parent 28909ac commit 893d2e5

File tree

7 files changed

+90
-35
lines changed

7 files changed

+90
-35
lines changed

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@
8989
<groupId>io.opentelemetry</groupId>
9090
<artifactId>opentelemetry-exporter-zipkin</artifactId>
9191
</dependency>
92+
<dependency>
93+
<groupId>org.zalando</groupId>
94+
<artifactId>logbook-spring-boot-starter</artifactId>
95+
<version>3.12.3</version>
96+
</dependency>
97+
<dependency>
98+
<groupId>org.zalando</groupId>
99+
<artifactId>logbook-logstash</artifactId>
100+
<version>3.12.3</version>
101+
</dependency>
92102

93103
<dependency>
94104
<groupId>org.apache.commons</groupId>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.xpinjection.library.config;
2+
3+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.zalando.logbook.HttpLogFormatter;
7+
import org.zalando.logbook.Sink;
8+
import org.zalando.logbook.logstash.LogstashLogbackSink;
9+
10+
@Configuration
11+
public class LogstashLogbookConfig {
12+
@Bean
13+
@ConditionalOnProperty(name = "logbook.format.style", havingValue = "json")
14+
Sink logstashSink(HttpLogFormatter formatter) {
15+
return new LogstashLogbackSink(formatter);
16+
}
17+
}

src/main/java/com/xpinjection/library/service/impl/BookServiceImpl.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
import java.util.concurrent.ConcurrentMap;
1717
import java.util.stream.Stream;
1818

19-
import static java.util.stream.Collectors.toList;
19+
import static net.logstash.logback.argument.StructuredArguments.value;
20+
import static net.logstash.logback.marker.Markers.append;
2021

2122
/**
2223
* @author Alimenkou Mikalai
@@ -42,11 +43,12 @@ public List<BookDto> addBooks(Books books) {
4243

4344
@Override
4445
public List<BookDto> findBooksByAuthor(String author) {
45-
LOG.info("Try to find books by author: {}", author);
46-
Assert.hasText(author, "Author is empty!");
47-
var normalizedAuthor = normalizeAuthorName(author);
48-
var books = cache.computeIfAbsent(normalizedAuthor, bookDao::findByAuthor).stream();
49-
return toDto(books);
46+
LOG.info(append("operation", "search"),
47+
"Try to find books by author: {}", value("author", author));
48+
Assert.hasText(author, "Author is empty!");
49+
var normalizedAuthor = normalizeAuthorName(author);
50+
var books = cache.computeIfAbsent(normalizedAuthor, bookDao::findByAuthor).stream();
51+
return toDto(books);
5052
}
5153

5254
@Override
@@ -76,7 +78,7 @@ private String splitOnFirstAndLastNames(String author) {
7678

7779
private List<BookDto> toDto(Stream<Book> books) {
7880
return books.map(BookServiceImpl::toDto)
79-
.collect(toList());
81+
.toList();
8082
}
8183

8284
public static BookDto toDto(Book book) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
logbook:
2+
format:
3+
style: json
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
logbook:
2+
format:
3+
style: json
4+
5+
logging:
6+
structured:
7+
format:
8+
console: ecs
9+
json:
10+
stacktrace:
11+
include-hashes: true
12+
include-common-frames: false
13+
root: first

src/main/resources/application.yaml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,17 @@ library:
7777
size: 0
7878

7979
logging:
80-
file:
81-
name: library.log
82-
structured:
83-
format:
84-
console: ecs
80+
level:
81+
org.zalando.logbook: TRACE
82+
83+
logbook:
84+
predicate:
85+
exclude:
86+
- path: /admin/**
87+
format:
88+
style: http
89+
strategy: body-only-if-status-at-least
90+
minimum-status: 200
8591

8692
springdoc:
8793
packages-to-scan: com.xpinjection.library.adaptors.api

src/main/resources/logback-spring.xml

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,42 @@
99
<appender name="JSON" class="ch.qos.logback.core.ConsoleAppender">
1010
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
1111
<providers>
12-
<contextName>
13-
<fieldName>app</fieldName>
14-
</contextName>
15-
<timestamp>
16-
<fieldName>ts</fieldName>
17-
<timeZone>UTC</timeZone>
18-
</timestamp>
19-
<logLevel>
20-
<fieldName>level</fieldName>
21-
</logLevel>
22-
<loggerName>
23-
<fieldName>logger</fieldName>
24-
</loggerName>
12+
<timestamp/>
13+
<threadName/>
14+
<logLevel/>
15+
<loggerName/>
16+
<contextName/>
2517
<callerData>
18+
<fieldName>caller</fieldName>
2619
<classFieldName>class</classFieldName>
2720
<methodFieldName>method</methodFieldName>
2821
<lineFieldName>line</lineFieldName>
2922
<fileFieldName>file</fileFieldName>
3023
</callerData>
31-
<threadName>
32-
<fieldName>thread</fieldName>
33-
</threadName>
34-
<mdc/>
3524
<arguments>
25+
<fieldName>args</fieldName>
3626
<includeNonStructuredArguments>false</includeNonStructuredArguments>
3727
</arguments>
38-
<stackTrace>
39-
<fieldName>stack</fieldName>
40-
</stackTrace>
41-
<message>
42-
<fieldName>msg</fieldName>
43-
</message>
28+
<mdc/>
29+
<tags/>
30+
<logstashMarkers/>
31+
<nestedField>
32+
<fieldName>exception</fieldName>
33+
<providers>
34+
<stackTrace/>
35+
<throwableClassName>
36+
<useSimpleClassName>false</useSimpleClassName>
37+
</throwableClassName>
38+
<throwableMessage/>
39+
<rootStackTraceElement/>
40+
<throwableRootCauseClassName>
41+
<useSimpleClassName>false</useSimpleClassName>
42+
</throwableRootCauseClassName>
43+
<throwableRootCauseMessage/>
44+
<stackHash/>
45+
</providers>
46+
</nestedField>
47+
<message/>
4448
</providers>
4549
</encoder>
4650
</appender>

0 commit comments

Comments
 (0)