Klogging Documentation
website·Indexed 32 days ago
https://klogging.ioA logging library for Kotlin and the JVM that supports coroutine context, multiple configuration formats including HOCON and JSON, and integration with Ktor, Spring Boot, SLF4J, and JDK Platform Logging.
What's inside Klogging
- Klogging is a pure-Kotlin logging library designed for flexibility and ease of use. It leverages Kotlin idioms for logger creation and event dispatching. It is specifically designed to integrate with Kotlin coroutines, making it suitable for asynchronous environments like the Ktor framework. Currently, Klogging only supports Kotlin on the JVM, with plans for future Kotlin Multiplatform target support.
Understand Klogging structured log events
Klogging utilizes log events instead of simple messages to capture richer information. Every log event includes inbuilt properties that describe the immediate context of where the event was generated, such as the timestamp, severity level, and logger name.Understand Klogging's asynchronous log processing architecture
Klogging uses Kotlin coroutines and channels to ensure log event handling is non-blocking. The architecture follows these primary asynchronous flows:
- Event Dispatching: Log events are sent into an events channel within a coroutine.
- Sink Distribution: A separate coroutine reads events from the events channel and dispatches them by sending them into the channels of each matching sink.
- Batching: For network destinations, events are read from sink channels in batches using dedicated coroutines to optimize throughput.
Use message templates for structured logging in Klogging
Klogging provides a partial implementation of the message templates standard, allowing developers to create structured log events. Currently, the following features of the standard are NOT supported:
- Escaping braces using double braces (e.g.,
{{) - Numeric property names (e.g.,
{0})
Full implementation of the standard is planned for future releases.
- Escaping braces using double braces (e.g.,
Include contextual information in logs using Coroutine Context
Klogging allows you to attach contextual information to a coroutine context, ensuring that all log events emitted within that scope (and any child coroutine scopes) automatically include these items. This is achieved using theLogContextclass, which maintains a map of contextual information within the coroutine context.Understand Klogging internal logger diagnostic levels
Klogging includes an internal logger used exclusively for diagnostics of the Klogging library itself. It emits string messages to the console based on the following levels:
- TRACE: Detailed tracing of log events through Klogging.
- DEBUG: Lifecycle events of Klogging components.
- INFO: Notifications that Klogging has read configuration from DSL or file.
- WARN: Errors encountered while interpreting configuration.
Understand the Klogging log event processing pipeline
Klogging uses Kotlin coroutine channels to move logging operations off the main execution threads. The process follows these stages:
- Filter: Determines if a log event should be constructed and emitted based on Klogger configuration.
- Emit: Sends the log event into the events channel (or directly to sinks).
- Dispatch: Identifies which sinks should receive the event and routes them to the corresponding sink channels.
- Render: Converts the log event into a format suitable for the specific sink.
- Send: Delivers the rendered event to the final destination.
Key implementation details:
Kloggerinstances usesuspendfunctions for emitting events.NoCoLoggerinstances launch coroutines to emit events into the channel (non-suspending).- Each sink has its own channel and processes events in the order received.
- Direct logging is possible, bypassing the asynchronous coroutine channels.
Integrate Klogging with other logging frameworks
Klogging can serve as the logging engine for other popular logging frameworks, allowing you to route logs from these facades through Klogging's back-end processing and destinations. It currently supports integration with:
- Simple Logging Facade for Java (SLF4J)
- Java Platform Logging API (JDK Platform Logging)
Configure Klogging for JVM logging
Klogging can be used as the logging library for the JVM and provides official support for the following integrations: JDK Platform Logging, Simple Logging Facade for Java (SLF4J), and Spring Boot. For users previously using the Hexagon microservices toolkit adapter, it is now recommended to use the JDK Platform Logging implementation.Klogging SLF4J compatibility in Spring Boot
Klogging acts as the logging framework for Spring Boot. Since all Spring libraries and existing application code typically log via SLF4J, all SLF4J log events are automatically processed by Klogging. Developers can also create structured log events using SLF4J.Configure Klogging using DSL, JSON, or HOCON
Klogging starts with no configuration and will not send log events until configured. Configuration can be implemented via three primary methods: using the Configuration domain-specific language (DSL) in code, using a JSON configuration file, or using a HOCON configuration file. Additionally, certain configurations can be overridden using environment variables.Understand Klogging Sinks and log destinations
In Klogging, Sinks are the final destinations for log events. A Sink is conceptually composed of two parts: a renderer that converts log events into aStringformat, and a sender that transmits those strings to a specific destination. Common supported destinations include standard output/error streams, Seq, Graylog, Logstash (ELK stack), and Splunk.