Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging using Log4J2 on aws lambda - Class not found

I am trying to use Log4J2 logging as described here in the AWS docs:

https://docs.aws.amazon.com/lambda/latest/dg/java-logging.html#java-wt-logging-using-log4j2.8

<?xml version="1.0" encoding="UTF-8"?>
<Configuration packages="com.amazonaws.services.lambda.runtime.log4j2.LambdaAppender">
  <Appenders>
    <Lambda name="Lambda">
      <PatternLayout>
          <pattern>%d{yyyy-MM-dd HH:mm:ss} %X{AWSRequestId} %-5p %c{1}:%L - %m%n</pattern>
      </PatternLayout>
    </Lambda>
  </Appenders>
  <Loggers>
    <Root level="debug">
      <AppenderRef ref="Lambda" />
    </Root>
  </Loggers>
</Configuration>

Error However I am getting the following error when running the lambda: (I removed timestamps below to improve readability)

ERROR Error processing element Lambda ([Appenders: null]): CLASS_NOT_FOUND
ERROR Unable to locate appender "Lambda" for logger config "root"

Tried I made sure that the log4J libs and log4j-core, log4j-api, aws-lambda-java-log4j2 and aws-lamda-java-core are all in the package.

like image 799
dvanrensburg Avatar asked Apr 23 '18 04:04

dvanrensburg


People also ask

Does AWS Lambda use Log4j?

The updated aws-lambda-java-log4j2 binary is available at the Maven repository and its source code is available in Github . To customize log output, support logging during unit tests, and log AWS SDK calls, use Apache Log4j 2 with SLF4J.

How do you implement Log4j2?

There are many ways to use Log4j2 configuration in you application. Using a configuration file written in XML, JSON, YAML or properties file. Programmatically, by creating a configuration factory and configuration implementation. Programmatically, by calling APIs exposed in the configuration interface.


1 Answers

I also had this problem. It turns out that there is a typo bug in the AWS example documentation.

The packages in the <Configuration .. tag is wrong.

According to the log4j plugin configuration docs the packages parameter is a package not a class.

So modify your log4j2.xml configuration to be...

<?xml version="1.0" encoding="UTF-8"?>
<Configuration packages="com.amazonaws.services.lambda.runtime.log4j2">
  <Appenders>
    <Lambda name="Lambda">
      <PatternLayout>
          <pattern>%d{yyyy-MM-dd HH:mm:ss} %X{AWSRequestId} %-5p %c{1}:%L - %m%n</pattern>
      </PatternLayout>
    </Lambda>
  </Appenders>
  <Loggers>
    <Root level="debug">
      <AppenderRef ref="Lambda" />
    </Root>
  </Loggers>
</Configuration>
like image 125
Karl Avatar answered Oct 13 '22 20:10

Karl