Log4j2 + AWS JAVA LAMBDA +云观察日志
我在代码中使用以下配置,但未生成 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
或者,您可以使用 Context 对象来获取记录器,如下所示
alternatively, You can use the Context object to get a logger like below
更改这些行:
问题
为我解决了这个
Changing these lines:
To
Solved this problem for me
只需添加此依赖项:
请注意,任何其他日志记录包都将被删除,slf4j 包也将被删除。
Just add this dependencies:
Be careful that any other logging packages are removed, also slf4j packages.
您所看到的未生成 cloudwatch 日志的问题是一个已知问题的产物,其中 log4j2 无法与 ShadowJar 或阴影 jar 正常工作。为了纠正这个问题并获得您期望的日志输出,您需要在构建时进行额外的转换。
使用 Gradle
为了尝试更快地修复此问题,我在下面链接了我用来纠正此问题的所有帖子。
原始帖子在
如何在 Maven 或 gradle 中修复的完整示例
使用 powertools 进行说明
解释有人声称问题是由于错误的路径名造成的(情况可能并非如此)
免责声明:对于删除我之前帖子的人 - 我发布的帖子不重复,但它们很相似,有一个它们之间的细微差别以及所有这些都有助于理解问题的各个方面。我花了很长时间才将所有这些随机信息整合到一个真正的修复中,我坚持我的答案与这篇文章相关。请不要删除旨在为其他人节省多个小时研究时间的帖子。尤其是当无法联系您、没有建议的编辑系统、也无法质疑您的意见时。请让社区通过投票系统来决定帖子的价值。
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
To try to more quickly remediate this issue I have linked all the posts I used to correct this below.
Original post that clued me in
Full example of how to fix in maven or gradle
Explanation with powertools
Explanation where someone is claiming the issue is due to a bad path name (that is probably not the case)
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.