https://github.com/idealo/logback-redis
Logback Redis Appender with Pipeline-Support for maximum throughput
https://github.com/idealo/logback-redis
logback-access logback-appender logback-redis
Last synced: 5 months ago
JSON representation
Logback Redis Appender with Pipeline-Support for maximum throughput
- Host: GitHub
- URL: https://github.com/idealo/logback-redis
- Owner: idealo
- License: apache-2.0
- Archived: true
- Created: 2016-06-15T10:36:56.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2022-06-29T22:19:03.000Z (almost 4 years ago)
- Last Synced: 2025-08-19T13:31:23.174Z (10 months ago)
- Topics: logback-access, logback-appender, logback-redis
- Language: Java
- Size: 93.8 KB
- Stars: 25
- Watchers: 19
- Forks: 10
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
> # **_IMPORTANT:_** PROJECT SUNSET AND ARCHIVED
No new maintainers found in https://github.com/idealo/logback-redis/issues/38
# Logback Redis Batch Appender
[](https://travis-ci.org/idealo/logback-redis)
[](https://maven-badges.herokuapp.com/maven-central/de.idealo.logback/logback-redis)
Enables Java applications to log directly to [redis](http://redis.io/) via the [jedis](https://github.com/xetorthio/jedis) client as part of centralized logging with the [ELK](https://www.elastic.co/products) stack.
More specifically, it uses [async appenders](https://github.com/logstash/logstash-logback-encoder#async) and [JSON encoding](https://github.com/logstash/logstash-logback-encoder#composite_encoder) of the [logstash-logback-encoder](https://github.com/logstash/logstash-logback-encoder) project. Messages are sent to redis in [batches](http://redis.io/topics/pipelining) for performance reasons. The [redis sentinel functionality](http://redis.io/topics/sentinel) is supported.
## Maven dependencies
``` xml
de.idealo.logback
logback-redis
1.5.0
```
used best in conjunction with
```xml
net.logstash.logback
logstash-logback-encoder
6.2
```
## Configuration
### Simple Configuration
```xml
...
SENTINEL
${sentinel.master.name}
${sentinel.host.list}
${sentinel.key}
${redis.ssl}
{
"@stage":"${STAGE}",
"app": "${projectName}",
"host": "${HOSTNAME}",
"key" : "userservices",
"level": "%level",
"logger": "%logger",
"message": "%message",
"thread": "%thread",
"timestamp": "%d{yyyy-MM-dd'T'HH:mm:ss.SSSZZ}"
}
...
```
### Parameters
* connectionConfig:
* key: key under which messages are stored in redis
* scheme (NODE | SENTINEL): defines whether redis is accessed via a single node or via [sentinel](http://redis.io/topics/sentinel)
* for scheme=SENTINEL:
* sentinelMasterName: name of the sentinel master
* sentinels: comma separated list of sentinels with the following structure: host1:port1,host2:port2
* for scheme=NODE:
* host: redis host
* port: redis port
* method (RPUSH | PUBLISH): defines the method to that should be used to send values to redis. with method PUBLISH the value defined as key is used as channel name. if method is omitted, then RPUSH is used.
* ssl: Whether to use SSL to communicate with redis (false or true, default is false). Your client and server certificates must be set up correctly.
* maxBatchMessages: number of messages which are sent as batch size to redis
* maxBatchSeconds: time interval in seconds after a batch of messages is sent to redis if the batch size is not reached
* encoder: encoder for JSON formatting of the messages
* ringBuffer and waitStrategyType determine [how the logstash-logback-encoder asynchronously processes the messages](https://github.com/logstash/logstash-logback-encoder#async). Note that messages may be lost if the ring buffer size is too small (["If the RingBuffer is full (e.g. due to slow network, etc), then events will be dropped."](https://github.com/logstash/logstash-logback-encoder#async)).
### Extended Configuration
``` xml
131072
SENTINEL
mymaster
server:26379
keyForRedis
1000
10
{
"timestamp": "%d{yyyy-MM-dd'T'HH:mm:ss.SSSZZ}",
"message": "%message",
"logger": "%logger",
"thread": "%thread",
"level": "%level",
"host": "${HOSTNAME}",
"file": "%file",
"line": "%line",
"app": "${projectName}"
}
30
4096
20
true
...
```
This appender configuration can either be included in a logback.xml file (via "included" tag) or be directly contained in a logback.xml (without "included" tag).
### JSON Format Created by the Appender (Example) (= Input for [Logstash](https://www.elastic.co/products/logstash))
``` js
{
"_index": "myIndex-2015.09.15.12",
"_type": "logs",
"_id": "AU_RAKiIuARjD1TqcEFe",
"_score": null,
"_source": {
"mdcKey1": "value1",
"mdcKey2": "value2",
"seq": "198",
"timestamp": "2015-09-15T14:35:19.256+0200",
"message": "logback-1:198",
"logger": "LoggingTest",
"thread": "main",
"level": "INFO",
"host": "myHost",
"file": "?",
"line": "?",
"@version": "1",
"@timestamp": "2015-09-15T12:35:25.251Z",
"type": "logs"
},
"fields": {
"@timestamp": [
1442320525251
]
},
"sort": [
1442320525251
]
}
```
### Logging Appender Errors to a File
The following logger configuration in the logback.xml is recommended in order to write error messages of the appender directly to a file in error situations (especially if redis is not available):
``` xml
```
## Shutdown
### Shutdown Hook
The redis batch appender must be shut down on application shutdown in order to ensure that cleans up background threads and pools and ensures that remaining messages are sent to Redis before shutting down the app. This is performed by the stop method of the redis batch appender that is automatically called when putting a shutdown hook in logback.xml:
``` xml
```
### [Spring Boot](http://projects.spring.io/spring-boot/) Apps
The shutdown hook above doesn't work in Spring Boot apps when they are shut down via the [actuator](http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#production-ready) shutdown URL ([POST {baseUrl}/shutdown](http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html)). Instead, this can be done by the following Spring component:
``` java
@Component
public class LogbackStopListener implements ApplicationListener {
@Override
public void onApplicationEvent(final ContextClosedEvent event) {
LogbackUtils.stopLogback();
}
}
```