Loki4j is the simplest way to push logs from your Java application to Loki and to connect them with all other metrics using Grafana dashboards. No extra tools needed, just add Loki4j appender to your Logback configuration and enjoy.
Quick Start
The current stable version of Loki4j requires Java 17+ and Logback v1.6.x. See the compatibility matrix for more information about older versions' support.
Add the following dependency to your project:
<dependency>
<groupId>com.github.loki4j</groupId>
<artifactId>loki-logback-appender</artifactId>
<version>2.1.0</version>
</dependency>
implementation 'com.github.loki4j:loki-logback-appender:2.1.0'
Then add Loki appender to your logback.xml:
<contextName>my-app</contextName>
<appender name="LOKI" class="com.github.loki4j.logback.Loki4jAppender">
<http>
<url>http://localhost:3100/loki/api/v1/push</url>
</http>
</appender>
<root level="DEBUG">
<appender-ref ref="LOKI" />
</root>
For more details, please refer to Docs.
Migrating from the previous Loki4j version? Read the Migration Guide.
If your company runs Loki4j in production, sponsoring the project is how you help keep it fast, current, and well maintained for the long run.
If you have found this project helpful, please drop a ☆ on GitHub.
Key Features:
Dynamic generation of Loki labels and metadata out of any Logback pattern, MDC, KVP, or SLF4J markers. Label values are specified as Logback patterns. This, along with MDC, KVP, and SLF4J markers' support, allows you to precisely control the label set for each particular log record. Learn more...
Structured metadata support. Unlike labels, structured metadata is not indexed in Loki. But it still can significantly increase search efficiency, as Loki does not have to scan entire message bodies for metadata. Loki4j provides the same capabilities of dynamic generation for structured metadata as it does for labels. Learn more...
Fast JSON layout for log message formatting. If you prefer Logstash-like log message formatting, you can switch the layout from plain text to JSON. Learn more...
Support of JSON and Protobuf Loki API flavors. By default, JSON endpoints are used, but you can switch to Protobuf API anytime. Learn more...
Compatibility with Grafana Cloud. Loki4j supports HTTP basic authentication, so you can use it for hosted Loki services (e.g., Grafana Cloud) as well as for on-premise Loki instances. See the example...
Zero-dependency. Loki4j jar has a very small footprint. It does not bring any new transitive dependencies to your project, assuming you already use
logback-classicfor logging. See the example...Performance metrics. You can monitor Loki4j's performance (e.g., encode/send duration, number of batches sent, etc.) by enabling instrumentation powered by Micrometer. Learn more...
Project Status
Loki4j is stable and actively maintained. The main logging features are implemented and the configuration API is settled, so releases now focus on:
- compatibility with new Grafana Loki versions
- security patches and dependency updates
- bug fixes and issue triage for teams running Loki4j in production