Log4j2 + AWS JAVA LAMBDA +云观察日志

发布于 2025-01-12 14:35:27 字数 2162 浏览 1 评论 0原文

我在代码中使用以下配置,但未生成 cloudwatch 日志。 Log4j2.xml 存在于 main/resources 中。我正在关注 AWS 文档和一些堆栈溢出,但我没有收到它所说的日志 -

Cloudwatch Logs

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
START RequestId: e36-c827-46-93-a7 Version: $LATEST
Transforming org/apache/logging/log4j/core/lookup/JndiLookup (lambdainternal.CustomerClassLoader@433d)
2022-03-07T16:01:51.527+05:30   2022-03-07 10:31:51,526 main ERROR Error processing element Lambda ([Appenders: null]): CLASS_NOT_FOUND
2022-03-07T16:01:51.626+05:30   2022-03-07 10:31:51,626 main ERROR Unable to locate appender "Lambda" for logger config "root"

Log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration packages="com.amazonaws.services.lambda.runtime.log4j2" status="WARN">
    <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="INFO">
            <AppenderRef ref="Lambda" />
        </Root>
        <Logger name="software.amazon.awssdk" level="WARN" />
        <Logger name="software.amazon.awssdk.request" level="DEBUG" />
    </Loggers>
</Configuration>

使用的 Gradle 依赖项:

implementation 'org.apache.logging.log4j:log4j-api:2.17.2'
implementation 'org.apache.logging.log4j:log4j-core:2.17.2'
runtimeOnly 'org.apache.logging.log4j:log4j-slf4j18-impl:2.17.0'
runtimeOnly 'com.amazonaws:aws-lambda-java-log4j2:1.5.1'

服务类日志声明

@Slf4j
public class ABC {
        
    public void method1(){
        log.info(" HELLO WORLD  ________ ");
    }
}

请帮忙。

I am using the following configurations in my code but cloudwatch logs are not getting generated. Log4j2.xml is present in main/resources. I am following the AWS documentation and some stack overflow but I am not getting the logs it says -

Cloudwatch Logs

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
START RequestId: e36-c827-46-93-a7 Version: $LATEST
Transforming org/apache/logging/log4j/core/lookup/JndiLookup (lambdainternal.CustomerClassLoader@433d)
2022-03-07T16:01:51.527+05:30   2022-03-07 10:31:51,526 main ERROR Error processing element Lambda ([Appenders: null]): CLASS_NOT_FOUND
2022-03-07T16:01:51.626+05:30   2022-03-07 10:31:51,626 main ERROR Unable to locate appender "Lambda" for logger config "root"

Log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration packages="com.amazonaws.services.lambda.runtime.log4j2" status="WARN">
    <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="INFO">
            <AppenderRef ref="Lambda" />
        </Root>
        <Logger name="software.amazon.awssdk" level="WARN" />
        <Logger name="software.amazon.awssdk.request" level="DEBUG" />
    </Loggers>
</Configuration>

Gradle Dependencies used:

implementation 'org.apache.logging.log4j:log4j-api:2.17.2'
implementation 'org.apache.logging.log4j:log4j-core:2.17.2'
runtimeOnly 'org.apache.logging.log4j:log4j-slf4j18-impl:2.17.0'
runtimeOnly 'com.amazonaws:aws-lambda-java-log4j2:1.5.1'

Service class Logging Declaration

@Slf4j
public class ABC {
        
    public void method1(){
        log.info(" HELLO WORLD  ________ ");
    }
}

Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

删除会话 2025-01-19 14:35:27

或者,您可以使用 Context 对象来获取记录器,如下所示

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public class MyClass implements RequestHandler<Map<String, Object>, Void> {
   @Override
public Void handleRequest(Map<String, Object> inputRequest, Context context) {
LambdaLogger logger = context.getLogger();
logger.log("Hello ");
}}

alternatively, You can use the Context object to get a logger like below

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public class MyClass implements RequestHandler<Map<String, Object>, Void> {
   @Override
public Void handleRequest(Map<String, Object> inputRequest, Context context) {
LambdaLogger logger = context.getLogger();
logger.log("Hello ");
}}
浅笑轻吟梦一曲 2025-01-19 14:35:27

更改这些行:

runtimeOnly 'org.apache.logging.log4j:log4j-slf4j18-impl:2.17.0'    
runtimeOnly 'com.amazonaws:aws-lambda-java-log4j2:1.5.1'

问题

implementation 'org.apache.logging.log4j:log4j-slf4j18-impl:2.17.0'
implementation 'com.amazonaws:aws-lambda-java-log4j2:1.5.1'

为我解决了这个

Changing these lines:

runtimeOnly 'org.apache.logging.log4j:log4j-slf4j18-impl:2.17.0'    
runtimeOnly 'com.amazonaws:aws-lambda-java-log4j2:1.5.1'

To

implementation 'org.apache.logging.log4j:log4j-slf4j18-impl:2.17.0'
implementation 'com.amazonaws:aws-lambda-java-log4j2:1.5.1'

Solved this problem for me

错爱 2025-01-19 14:35:27

只需添加此依赖项:

            <dependency>
                <groupId>com.amazonaws</groupId>
                <artifactId>aws-lambda-java-log4j2</artifactId>
                <version>1.5.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>2.20.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api</artifactId>
                <version>2.20.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-slf4j-impl</artifactId>
                <version>2.20.0</version>
            </dependency>

请注意,任何其他日志记录包都将被删除,slf4j 包也将被删除。

Just add this dependencies:

            <dependency>
                <groupId>com.amazonaws</groupId>
                <artifactId>aws-lambda-java-log4j2</artifactId>
                <version>1.5.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>2.20.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api</artifactId>
                <version>2.20.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-slf4j-impl</artifactId>
                <version>2.20.0</version>
            </dependency>

Be careful that any other logging packages are removed, also slf4j packages.

南薇 2025-01-19 14:35:27

您所看到的未生成 cloudwatch 日志的问题是一个已知问题的产物,其中 log4j2 无法与 ShadowJar 或阴影 jar 正常工作。为了纠正这个问题并获得您期望的日志输出,您需要在构建时进行额外的转换。

使用 Gradle

import com.github.jengelman.gradle.plugins.shadow.transformers.Log4j2PluginsCacheFileTransformer

shadowJar{
    transform(Log4j2PluginsCacheFileTransformer)
}

为了尝试更快地修复此问题,我在下面链接了我用来纠正此问题的所有帖子。

免责声明:对于删除我之前帖子的人 - 我发布的帖子不重复,但它们很相似,有一个它们之间的细微差别以及所有这些都有助于理解问题的各个方面。我花了很长时间才将所有这些随机信息整合到一个真正的修复中,我坚持我的答案与这篇文章相关。请不要删除旨在为其他人节省多个小时研究时间的帖子。尤其是当无法联系您、没有建议的编辑系统、也无法质疑您的意见时。请让社区通过投票系统来决定帖子的价值。

The issue that you are seeing with cloudwatch logs not being generated is an artifact of a known issue where log4j2 does not work correctly with shadowJar or a shaded jar. In order to correct this and get the log output you expect you need an additional transformation at build time.

Using Gradle

import com.github.jengelman.gradle.plugins.shadow.transformers.Log4j2PluginsCacheFileTransformer

shadowJar{
    transform(Log4j2PluginsCacheFileTransformer)
}

To try to more quickly remediate this issue I have linked all the posts I used to correct this below.

Disclaimer: To the person who deleted my previous post - the posts that I posted on are not duplicates but they are similar, there is a nuanced difference between them and all of them helped understanding various aspects of the problem. It took a very long time to put together all these random pieces of info into a true fix and I stand by my answer as being relevant on this post. Please do not remove posts that are geared towards saving multiple hours of research for others. Especially when there is no way to contact you, no suggested edit system, and no way to dispute YOUR opinion. Please allow the community to decide how valuable a post is by the vote system.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文