Menu
AWS Lambda
Developer Guide

Using Earlier Custom Appender for Log4j™ 1.2 (Not Recommended)

Note

Support for the Log4j v1.2 custom appender is marked for End-Of-Life. It will not receive ongoing updates and is not recommended for use. For more information, see Log4j 1.2

AWS Lambda supports Log4j 1.2 by providing a custom appender. You can use the custom Log4j (see Apache log4j 1.2) appender provided by Lambda for logging from your lambda functions. Every call to Log4j methods, such as log.debug() or log.error(), will result in a CloudWatch Logs event. The custom appender is called LambdaAppender and must be used in the log4j.properties file. You must include the aws-lambda-java-log4j artifact (artifactId:aws-lambda-java-log4j) in the deployment package (.jar file). For an example, see Example: Writing Logs Using Log4J v1.2 (Not Recommended) .

Example: Writing Logs Using Log4J v1.2 (Not Recommended)

Note

Versions 1.x of Log4j have been marked as end of life. For more information, see Log4j 1.2

The following Java code example writes logs using both the System methods and Log4j to illustrate how they differ when AWS Lambda logs information to CloudWatch.

Copy
package example; import com.amazonaws.services.lambda.runtime.Context; import org.apache.logging.log4j.Logger; public class Hello { // Initialize the Log4j logger. static final Logger log = Logger.getLogger(Hello.class); public String myHandler(String name, Context context) { // System.out: One log statement but with a line break (AWS Lambda writes two events to CloudWatch). System.out.println("log data from stdout \n this is continuation of system.out"); // System.err: One log statement but with a line break (AWS Lambda writes two events to CloudWatch). System.err.println("log data from stderr. \n this is a continuation of system.err"); // Use log4j to log the same thing as above and AWS Lambda will log only one event in CloudWatch. log.debug("log data from log4j debug \n this is continuation of log4j debug"); log.error("log data from log4j err. \n this is a continuation of log4j.err"); // Return will include the log stream name so you can look // up the log later. return String.format("Hello %s. log stream = %s", name, context.getLogStreamName()); } }

The example uses the following log4j.properties file (project-dir/src/main/resources/ directory).

Copy
log = . log4j.rootLogger = DEBUG, LAMBDA #Define the LAMBDA appender log4j.appender.LAMBDA=com.amazonaws.services.lambda.runtime.log4j.LambdaAppender log4j.appender.LAMBDA.layout=org.apache.log4j.PatternLayout log4j.appender.LAMBDA.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss} <%X{AWSRequestId}> %-5p %c{1}:%m%n

The following is sample of log entries in CloudWatch Logs.

Note:

  • AWS Lambda parses the log string in each of the System.out.println() and System.err.println() statements logs as two separate events (note the two down arrows in the screenshot) because of the line break.

  • The Log4j methods (log.debug() and log.error()) produce one CloudWatch event.

  • AWS Lambda runtime adds the AWSRequestId in the MDC to the log4j context (see Class MDC for log4j v 1.2 and Class ThreadContext). To get this value in the log as shown, we added %X{AWSRequestId} in the conversion pattern in the log4.properties file.

You can do the following to test the code:

  • Using the code, create a deployment package. In your project, don't forget to add the log4j.properties file in the project-dir/src/main/resources/ directory.

  • Upload the deployment package to AWS Lambda to create your Lambda function.

  • To test your Lambda function use a string ("this is a test") as sample event. The handler code receives the sample event but does nothing with it. It only shows how to write logs.

Follow the instructions provided in the Getting Started. For more information, see Step 2.3: (Optional) Create a Lambda Function Authored in Java. Note the following differences:

  • When you create a deployment package, don't forget the aws-lambda-java-log4j dependency for Log4j 1.2 dependency.

  • When you create the Lambda function, specify example.Hello::myHandler (package.class::method) as the handler value.