4
4
[ ![ Discord] ( https://img.shields.io/discord/1074074312421683250?color=%237289da&label=discord )] ( https://discord.gg/Qcqf9R27BR )
5
5
6
6
# avaje-logback-encoder
7
- logback encoder that uses avaje-jsonb to log events as json
7
+ Logback encoder that log events as json (similar to Logstash).
8
8
9
- ## Usage
9
+ Example:
10
+ ``` json
11
+ {"timestamp" :" 2025-01-10T14:47:42.313+13:00" ,"level" :" INFO" ,"logger" :" org.example.Foo" ,"message" :" Hi" ,"thread" :" main" }
12
+ ```
13
+
14
+ Example with component and environment:
15
+ ``` json
16
+ {"component" :" my-component" ,"env" :" DEV" ,"timestamp" :" 2025-01-10T14:47:42.313+13:00" ,"level" :" INFO" ,"logger" :" org.example.Foo" ,"message" :" Hi" ,"thread" :" main" }
17
+ ```
18
+
19
+ ## Fields
20
+ #### Standard Fields
21
+ - ` timestamp `
22
+ - ` level `
23
+ - ` logger `
24
+ - ` message `
25
+ - ` thread `
26
+ - ` stacktrace `
27
+
28
+ #### MDC Fields
29
+ MDC key/values are included in logged events.
30
+
31
+ #### Custom Fields
32
+ Extra Custom fields can be declared in JSON form, these are added to all logged events.
33
+
34
+ #### Extra recommended Fields
35
+ - ` component ` - Use to define the "component" (approximately application or a specific component of an application)
36
+ - ` env ` - Use to define the "environment" such as dev, test, prod etc
37
+
38
+ These default by reading System Environment variables ` COMPONENT ` and ` ENVIRONMENT ` respectively and
39
+ can also be explicitly set via configuration.
40
+
41
+
42
+
43
+ # How to use
44
+
45
+ #### 1 - Add dependency
46
+
47
+ For Java 11+ and Logback 1.2.x+ use version ` 1.0 ` of the dependency:
48
+ ``` xml
49
+ <dependency >
50
+ <groupId >io.avaje</groupId >
51
+ <artifactId >avaje-logback-encoder</artifactId >
52
+ <version >1.0</version >
53
+ </dependency >
54
+ ```
10
55
11
- Add the encoder to your appender
56
+ For Java 8 and Logback 1.1.x use version ` 1.0-java8 ` of the dependency.
57
+ ``` xml
58
+ <dependency >
59
+ <groupId >io.avaje</groupId >
60
+ <artifactId >avaje-logback-encoder</artifactId >
61
+ <version >1.0-java8</version >
62
+ </dependency >
63
+ ```
12
64
65
+ #### 2 - Use the Encoder in logback.xml
66
+
67
+ In ` logback.xml ` specify JsonEncoder as the encoder like:
13
68
``` xml
69
+ <appender name =" app" class =" your.appender.class" >
70
+ <encoder class =" io.avaje.logback.encoder.JsonEncoder" />
71
+ </appender >
72
+ ```
14
73
74
+ Optionally, configure with ` component ` and ` environment ` like:
75
+ ``` xml
15
76
<appender name =" app" class =" your.appender.class" >
16
- <encoder class =" io.avaje.logback.encoder.JsonEncoder" >
17
- <-- configuration -->
18
- </encoder >
77
+ <encoder class =" io.avaje.logback.encoder.JsonEncoder" >
78
+ <component >my-component</component > <!-- OPTIONAL -->
79
+ <enviroment >dev</enviroment > <!-- OPTIONAL -->
80
+ </encoder >
19
81
</appender >
20
82
```
21
83
22
- ### JPMS Use
84
+ Optionally specify custom fields that will appear in every LoggingEvent like:
85
+ ``` xml
86
+ <encoder class =" io.avaje.logback.encoder.JsonEncoder" >
87
+ <customFields >{"appname":"myWebservice","roles":["orders","auth"]}</customFields >
88
+ </encoder >
89
+ ```
90
+
91
+ #### AWS Lambda / StdOutAppender
92
+
93
+ For AWS Lambda log events are written to ` System.out ` and so this also provides
94
+ an appender that defaults to using JsonEncoder to write log events to ` System.out ` .
95
+ We can specify to use that appender like:
96
+
97
+ ``` xml
98
+ <!-- Defaults to using the JsonEncoder -->
99
+ <appender class =" io.avaje.logback.encoder.StdOutAppender" />
100
+ ```
101
+
102
+ Or with configuration options for component and environment like:
103
+
104
+ ``` xml
105
+ <appender class =" io.avaje.logback.encoder.StdOutAppender" >
106
+ <component >my-foo</component >
107
+ </appender >
108
+ ```
109
+
110
+ Or with an encoder (potentially a different encoder):
111
+
112
+ ``` xml
113
+ <appender class =" io.avaje.logback.encoder.StdOutAppender" >
114
+ <encoder class =" io.avaje.logback.encoder.JsonEncoder" >
115
+ <component >my-foo</component >
116
+ <environment >prod</environment >
117
+ <customFields >{"appname":"myWebservice","buildinfo":42, "roles":["customerorder","auth"],"f":true}</customFields >
118
+ </encoder >
119
+ </appender >
120
+ ```
121
+
122
+
123
+ ## Java modules
23
124
To ensure ` jlink ` correctly determines the runtime modules required, add the following to your ` module-info.java ` :
24
125
25
126
``` java
@@ -28,18 +129,7 @@ module my.module {
28
129
}
29
130
```
30
131
31
- ## Global Custom Fields
32
-
33
- Add custom fields that will appear in every LoggingEvent like this :
34
-
35
- ``` xml
36
132
37
- <encoder class =" io.avaje.logback.encoder.JsonEncoder" >
38
- <customFields >{"appname":"myWebservice","roles":["customerorder","auth"],"buildinfo":{"version":"Version
39
- 0.1.0-SNAPSHOT","lastcommit":"75473700d5befa953c45f630c6d9105413c16fe1"}}
40
- </customFields >
41
- </encoder >
42
- ```
43
133
44
134
## Customizing Timestamp
45
135
@@ -50,7 +140,6 @@ By default, timestamps are written as string values in the format specified by
50
140
You can change the pattern like this:
51
141
52
142
``` xml
53
-
54
143
<encoder class =" io.avaje.logback.encoder.JsonEncoder" >
55
144
<timestampPattern >yyyy-MM-dd'T'HH:mm:ss.SSS</timestampPattern >
56
145
</encoder >
@@ -64,7 +153,6 @@ The value of the `timestampPattern` can be any of the following:
64
153
The formatter uses the default TimeZone of the host Java platform by default. You can change it like this:
65
154
66
155
``` xml
67
-
68
156
<encoder class =" io.avaje.logback.encoder.JsonEncoder" >
69
157
<timeZone >UTC</timeZone >
70
158
</encoder >
0 commit comments