@@ -22,6 +22,11 @@ Example for Maven pom.xml
22
22
<artifactId >log4j-api</artifactId >
23
23
<version >2.17.1</version >
24
24
</dependency >
25
+ <dependency >
26
+ <groupId >org.apache.logging.log4j</groupId >
27
+ <artifactId >log4j-layout-template-json</artifactId >
28
+ <version >2.17.1</version >
29
+ </dependency >
25
30
....
26
31
</dependencies >
27
32
```
@@ -108,7 +113,7 @@ Add the following file `<project-dir>/src/main/resources/log4j2.xml`
108
113
</Lambda >
109
114
</Appenders >
110
115
<Loggers >
111
- <Root level =" info " >
116
+ <Root level =" ${env:AWS_LAMBDA_LOG_LEVEL:-INFO} " >
112
117
<AppenderRef ref =" Lambda" />
113
118
</Root >
114
119
</Loggers >
@@ -124,6 +129,8 @@ package example;
124
129
125
130
import com.amazonaws.services.lambda.runtime.Context ;
126
131
132
+ import static org.apache.logging.log4j.CloseableThreadContext.put ;
133
+ import org.apache.logging.log4j.CloseableThreadContext.Instance ;
127
134
import org.apache.logging.log4j.LogManager ;
128
135
import org.apache.logging.log4j.Logger ;
129
136
@@ -143,6 +150,12 @@ public class Hello {
143
150
144
151
logger. error(" log data from log4j err. \n this is a continuation of log4j.err" );
145
152
153
+ // When logging in JSON, you can also add custom fields
154
+ // In java11+ you can use the `try (var ctx = put("name", name)) {}` structure
155
+ Instance ctx = put(" name" , name);
156
+ logger. info(" log line with input name" );
157
+ ctx. close();
158
+
146
159
// Return will include the log stream name so you can look
147
160
// up the log later.
148
161
return String . format(" Hello %s. log stream = %s" , name, context. getLogStreamName());
0 commit comments