Logging using Log4J2 on aws lambda - Class not found
Asked Answered
O

3

21

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.

Octodecillion answered 23/4, 2018 at 4:30 Comment(0)
E
36

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>
Edda answered 23/4, 2018 at 4:37 Comment(6)
Thanks I'll give this a go!Octodecillion
Wasted 3 hours on this :(Frankfrankalmoign
yeah. was very annoying, and the fix to doco is still not merged in upstream!Edda
update, it now looks like the official doco at docs.aws.amazon.com/lambda/latest/dg/java-logging.html has now beenupdated and is correct :-)Edda
the official documentation as of today https://docs.aws.amazon.com/lambda/latest/dg/java-logging.html does not have the packages attribute in the Configuration tag. It worked for me only after I added the packages attribute. Thanks to the OP would never have figured it out myself otherwise.Zwieback
I also had the same error despite correct log4j and dependency configuration. You also have to make sure that the log4j related artifacts (log4j*, aws-lambda*) do not get classes excluded by maven-shade-plugin. The logging related stuff is particularly prone to this because the path to their usage might be through xml config files and not a compile time analysis of java code.Advert
A
2

Despite there already being an accepted answer there is a different solution. If you are using AWS Lambda logging with Log4j and have following dependency

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-lambda-java-log4j2</artifactId>
</dependency>

and are using Maven shade plugin, then you must configure the Maven shade plugin as follows:

<plugins>
  ...
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.3</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <transformers>
            <transformer
                    implementation="com.github.edwgiz.mavenShadePlugin.log4j2CacheTransformer.PluginsCacheFileTransformer">
            </transformer>
          </transformers>
        </configuration>
      </execution>
    </executions>
    <dependencies>
      <dependency>
        <groupId>com.github.edwgiz</groupId>
        <artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
        <version>2.8.1</version>
      </dependency>
    </dependencies>
  </plugin>
  ...
</plugins>

The reason for that is that there are open issues in Log4j2, which prevents loading of Log4j plugins when using Maven shading.

More details in following sites:

Accipitrine answered 14/2, 2023 at 7:16 Comment(0)
P
0

To try to tie all of these posts together and require less digging @TheCoolDrop has the real answer here. I tried the accepted answer fix of the package name being wrong and saw some folks complaining the doc was never updated but I don't think that fix works and I think the doc is correct and the package declaration is not necessary.

This question is for lambda which requires using shadowJar (or a shaded jar) which does not play nice with log4j2. It is a known (albeit somewhat hard to find / determine) issue. One of the reasons it is so hard to understand is that your logging config just doesn't work, no errors, and you still get logs ... just not what you are expecting.

Again @TheCoolDrop I believe has the real solution above but you can also see the following.

Potshot answered 13/7, 2023 at 17:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.