- Kotlin 100%
LT :: Logging
for Kotlin apps. built on slf4j/logback, with various integrations for easy I/O integration
Integrations
- servlet: Default Java Servlet API. Register
LoggingFilterto get inbound http logging - Spring AOP: An aspect to provide annotation based logging
- SpringBoot starter: Autoconfiguration for Spring Boot
- OkHttp: An
Interceptorto log outgoing http - Spring RestTemplate: An
ClientHttpRequestInterceptorto log outgoing http
Usage
- Add the BOM
<dependencyManagement> <dependency> <groupId>de.loosetie.logging</groupId> <artifactId>lt-logging-bom</artifactId> <version>{version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencyManagement> - Choose modules to use
<dependencies> <dependency> <groupId>de.loosetie.logging</groupId> <artifactId>lt-logging-servlet</artifactId> </dependency> <dependency> <groupId>de.loosetie.logging</groupId> <artifactId>lt-logging-spring-aop</artifactId> </dependency> </dependencies> - For Spring Boot the
lt-logging-spring-starterwill provide a basic configuration. Property support is provided with the prefixlt.loggingusing the spring way
Log
The interface Log is a layer over slf4j/logback to provide asynchronous message building.
import de.loosetie.logging.logFactory
class Example {
val log by logFactory()
}
This will configure the logger for class Example, additionally LogFactory.getLogger is available if more customization is needed.
RequestId
The RequestId is structured hierarchically, so you can follow the context build by your logging needs.
Pattern: <AppId>[-<Prefix><Sequence>...]
- AppId: A random Integer generated at startup
- Prefix: A customizable prefix to quickly identify your context (keep it short :))
- Sequence: A number counted for every generated
RequestIdin the same scope Prefix and Sequence may be repeated, if aRequestIdis generated at a sub-scope
Examples:
8ee36c0c-h1 # HttpFilter get a request
8ee36c0c-h1-s0 # A service is called
8ee36c0c-h1-s0-db1 # DB access
8ee36c0c-h1-s0-db2
8ee36c0c-h1-s0 # Return of the service processing
8ee36c0c-h1-s1 # Second service
8ee36c0c-h1-s1-r0 # REST call
8ee36c0c-h1
LogIt
An annotation provided by lt-logging-spring-aop allows you to add logs to all spring beans annotated with @LogIt. This can be done on function or class level for public members.
Hint: As for all aspect oriented features this only works on open/non-final functions.
Coroutines
As kotlin coroutines are a separate concept to threads, watch out for the right context.
For this purpose you can use LoggingContext:
import de.loosetie.logging.coroutines.LoggingContext
//...
fun example() {
runBlocking {
launch(Dispatchers.Default + LoggingContext()) {
// Slf4j MDC and RequestId context is fine here
}
}
}
See also: kotlinx-coroutines-slf4j