@@ -22,6 +22,11 @@ Example for Maven pom.xml
2222 <artifactId >log4j-api</artifactId >
2323 <version >2.17.1</version >
2424 </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 >
2530 ....
2631</dependencies >
2732```
@@ -108,7 +113,7 @@ Add the following file `<project-dir>/src/main/resources/log4j2.xml`
108113 </Lambda >
109114 </Appenders >
110115 <Loggers >
111- <Root level =" info " >
116+ <Root level =" ${env:AWS_LAMBDA_LOG_LEVEL:-INFO} " >
112117 <AppenderRef ref =" Lambda" />
113118 </Root >
114119 </Loggers >
@@ -124,6 +129,8 @@ package example;
124129
125130import com.amazonaws.services.lambda.runtime.Context ;
126131
132+ import static org.apache.logging.log4j.CloseableThreadContext.put ;
133+ import org.apache.logging.log4j.CloseableThreadContext.Instance ;
127134import org.apache.logging.log4j.LogManager ;
128135import org.apache.logging.log4j.Logger ;
129136
@@ -143,6 +150,12 @@ public class Hello {
143150
144151 logger. error(" log data from log4j err. \n this is a continuation of log4j.err" );
145152
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+
146159 // Return will include the log stream name so you can look
147160 // up the log later.
148161 return String . format(" Hello %s. log stream = %s" , name, context. getLogStreamName());
0 commit comments