This 5 minutes library will make your logs cleaner and easier to read / navigate.
This is not one more logging library - we just wrap slf4j
api and make it more strict.
This Java library will split your events from params. This will make your logs easier to read / write / navitage
Here is most commonly used style to log process:
My service is sending mail "Hello world" to earth@gmail.com
Why we think that this log entry is bad?
- It's hard to split event (
sending mail
) from params (subject -Hello world
, recipient -earch@gmail.com
) - It will be pretty hard to find this row in logs
- It's not possible to parse
- It requires us to construct correct English sentence
- Let's split event and params
- (optional) Let's write event in a short way
mail.send: {subject=hello world, recipient=earch@gmail.com}
Please note that we use short (technical) way to describe event. Instead of writing human readable phrase (like "We are sending mail") we use short event name ("mail.send") To construct your best event name we suggest: you to
- form event names same as package names - from most common to most specific.
- don't use spaces
- split words with upper case /
-
/_
(e.g.user.register.e.notUnique
)- split parts with
.
We pass params as a map. You need a simple way to create maps. In examples below we will use to.map but you are free to use any other convenient way to create maps (e.g. Guava ImmutableMap.of).
<dependency>
<groupId>io.thedocs</groupId>
<artifactId>soyuz-loge</artifactId>
<version>1.2</version>
</dependency>
repositories {
mavenCentral()
}
dependencies {
compile 'io.thedocs:soyuz-loge:1.2'
}
Always use
loge
as variable name
This example depends on soyuz-is-to
package io.thedocs.soyuz.log;
import io.thedocs.soyuz.to;
public class LoggerEventsExamples {
private static final LoggerEvents loge = LoggerEvents.getInstance(LoggerEventsExamples.class);
public static void main(String[] args) {
Exception e = new IllegalStateException();
//user.register.e.notUnique: {mail=hello@gmail.com}
loge.warn("user.register.e.notUnique", to.map("mail", "hello@gmail.com"));
//user.login: {mail=hello@gmail.com}
loge.debug("user.login", to.map("mail", "hello@gmail.com"));
//task.created: {label=Improve documentation, type=todo, user=hello@gmail.com}
loge.info("task.created", to.map("user", "hello@gmail.com", "label", "Improve documentation", "type", "todo"));
//task.process.e: {id=123, type=todo}
loge.error("task.process.e", to.map("id", 123, "type", "todo"), e);
}
}