Spring Boot Contextual Logging


License

License

Categories

Categories

Spring Boot Container Microservices Logging Application Layer Libs
GroupId

GroupId

com.github.timpeeters
ArtifactId

ArtifactId

spring-boot-contextual-logging
Last Version

Last Version

2.2.3
Release Date

Release Date

Type

Type

jar
Description

Description

Spring Boot Contextual Logging
Spring Boot Contextual Logging
Project URL

Project URL

https://github.com/timpeeters/spring-boot-contextual-logging
Source Code Management

Source Code Management

https://github.com/timpeeters/spring-boot-contextual-logging

Download spring-boot-contextual-logging

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.timpeeters/spring-boot-contextual-logging/ -->
<dependency>
    <groupId>com.github.timpeeters</groupId>
    <artifactId>spring-boot-contextual-logging</artifactId>
    <version>2.2.3</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.timpeeters/spring-boot-contextual-logging/
implementation 'com.github.timpeeters:spring-boot-contextual-logging:2.2.3'
// https://jarcasting.com/artifacts/com.github.timpeeters/spring-boot-contextual-logging/
implementation ("com.github.timpeeters:spring-boot-contextual-logging:2.2.3")
'com.github.timpeeters:spring-boot-contextual-logging:jar:2.2.3'
<dependency org="com.github.timpeeters" name="spring-boot-contextual-logging" rev="2.2.3">
  <artifact name="spring-boot-contextual-logging" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.timpeeters', module='spring-boot-contextual-logging', version='2.2.3')
)
libraryDependencies += "com.github.timpeeters" % "spring-boot-contextual-logging" % "2.2.3"
[com.github.timpeeters/spring-boot-contextual-logging "2.2.3"]

Dependencies

compile (12)

Group / Artifact Type Version
ch.qos.logback : logback-classic jar
ch.qos.logback : logback-core jar
org.slf4j : slf4j-api jar
org.springframework : spring-beans jar
org.springframework : spring-context jar
org.springframework : spring-core jar
org.springframework : spring-web jar
org.springframework.boot : spring-boot jar
org.springframework.boot : spring-boot-autoconfigure jar
org.springframework.boot : spring-boot-configuration-processor Optional jar
org.springframework.boot : spring-boot-starter-logging jar
org.springframework.cloud : spring-cloud-context Optional jar

provided (1)

Group / Artifact Type Version
javax.servlet : javax.servlet-api jar

test (7)

Group / Artifact Type Version
junit : junit jar
org.apache.tomcat : tomcat-annotations-api jar
org.assertj : assertj-core jar
org.springframework : spring-test jar
org.springframework.boot : spring-boot-test jar
org.springframework.boot : spring-boot-starter-test jar
org.springframework.boot : spring-boot-starter-web jar

Project Modules

There are no modules declared in this project.

Spring Boot Contextual Logging

Known Vulnerabilities Dependabot Status Maven Central

This project allows to configure Spring Boot logging on a per-request basis. Currently this project only supports Logback, the default logging framework in Spring Boot.

Versions

Multiple branches are maintained to support multiple Spring Boot versions. The following tables show the relation between the Spring Boot version and the Spring Boot Contextual Logging version.

Spring Boot Spring Boot Contextual Logging Branch
1.5.x 1.0.x 1.0.x
2.0.x 2.0.x 2.0.x
2.1.x 2.1.x 2.1.x
2.2.x 2.2.x master

Installation

  1. Add the following Maven dependency.
<dependency>
    <groupId>com.github.timpeeters</groupId>
    <artifactId>spring-boot-contextual-logging</artifactId>
    <version>X.X.X</version>
</dependency>

Implementation details

ContextualLoggingLogbackFilter

The implementation relies on the use of a Logback TurboFilter.

TurboFilters are called every time a logging request is issued. They are called before the LoggingEvent is created. As such, TurboFilters are intended for high performance filtering of logging events.

This projects ContextualLoggingLogbackFilter extends the Logback TurboFilter abstract class and checks for the presence of a ContextualLoggingContext in the ContextualLoggingContextHolder. If a ContextualLoggingContext is present, it will use the state in the context to decide whether a log statement should be filtered or not. If no ContextualLoggingContext is present, the filter will return a FilterReply.NEUTRAL resulting in default Logback behavior.

RequestEvaluator

The RequestEvaluator interface is responsible for deciding whether contextual logging should be enabled for a given request or not.

The interface takes only one input parameter HttpServletRequest and returns a boolean indicating whether contextual logging should be enabled or not.

boolean shouldEnable(HttpServletRequest request);

By default a HeaderRequestEvaluator is created that looks for the presence of a request header with the name Contextual-Logging. If the header is present, contextual logging is enabled.

@Bean
@ConditionalOnMissingBean
@ConditionalOnWebApplication
public RequestEvaluator requestEvaluator() {
    return new HeaderRequestEvaluator();
}

It is possible to register you own RequestEvaluator using different logic. E.g. evaluating the presence of a request parameter.

@Bean
public RequestEvaluator requestEvaluator() {
    return request -> request.getParameter("debug") != null;
}

LogLevelSource

When contextual logging is enabled for a request, the LogLevelSource interface will determine what log levels should be applied.

The interface takes only one input parameter HttpServletRequest and returns a map with the logger name as key and the log level as value.

Map<String, LogLevel> getLogLevels(HttpServletRequest request);

By default two implementations of the interface are shipped: HeaderLogLevelSource and PropertiesLogLevelSource.

HeaderLogLevelSource

Retrieves the log levels from the HTTP headers of the request. This implementation is enabled by default.

Configuration:

contextual.logging.enabled=true
contextual.logging.log-level-source=header

E.g. the following headers will result in info level for all loggers and debug level for the com.github logger.

GET / HTTP/1.1
Host: localhost
ContextualLogging: root=info
ContextualLogging: com.github=debug

PropertiesLogLevelSource

Retrieves the log levels from the Spring Environment.

Enabled using the following configuration:

contextual.logging.enabled=true
contextual.logging.log-level-source=properties
contextual.logging.level.root=info
contextual.logging.level.com.github=debug

References

Versions

Version
2.2.3
2.2.2
2.2.1
2.2.0
2.1.1
2.1.0
2.0.3
2.0.2
2.0.1
2.0.0
1.0.8
1.0.7
1.0.6
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0