java 使用 slf4j 登录 AWS Lambda
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46080448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Logging in AWS Lambda with slf4j
提问by chrisrhyno2003
I am using a lambda function and writing it in Java. I was looking up logging for Lambda functions when I read the docs and they support log4j - http://docs.aws.amazon.com/lambda/latest/dg/java-logging.html#java-wt-logging-using-log4j.
我正在使用 lambda 函数并用 Java 编写它。我在阅读文档时正在查找 Lambda 函数的日志记录,它们支持 log4j - http://docs.aws.amazon.com/lambda/latest/dg/java-logging.html#java-wt-logging-using-日志4j。
I was wondering if we could use logging using the Slf4j annotation as well since Slf4j is only a binding annotation. Has anybody tried using Slf4j before with lambda?
我想知道我们是否也可以使用 Slf4j 注释来使用日志记录,因为 Slf4j 只是一个绑定注释。有没有人在 lambda 之前尝试过使用 Slf4j?
采纳答案by foal
Yes, you can. Just add the following dependencies to your project:
是的你可以。只需将以下依赖项添加到您的项目中:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-log4j</artifactId>
<version>1.0.0</version>
</dependency>
and create correct log4j.properties
in /src/main/resources/
of your project, e.g.
并log4j.properties
在/src/main/resources/
您的项目中创建正确的,例如
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
回答by Igor Akkerman
The jlib AWS Lambda Logback Appenderallows you to use SLF4Jwith Logbackfrom your AWS Lambda functions.
该jlib AWS拉姆达的logback追加程序允许您使用SLF4J使用的logback从您的AWS lambda函数。
Simply add these dependencies:
只需添加这些依赖项:
Gradle (build.gradle)
Gradle ( build.gradle)
dependencies {
implementation 'org.slf4j:slf4j-api:1.8.0-beta2'
runtimeOnly 'org.jlib:jlib-awslambda-logback:1.0.0'
}
Maven (pom.xml)
Maven ( pom.xml)
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.8.0-beta2</version>
</dependency>
<dependency>
<groupId>org.jlib</groupId>
<artifactId>jlib-awslambda-logback</artifactId>
<version>1.0.0</version>
<scope>runtime</scope>
</dependency>
Then use the AwsLambdaAppender
in your logging configuration:
然后AwsLambdaAppender
在您的日志记录配置中使用:
Example XML configuration (src/main/resources/logback.xml)
XML 配置示例(src/main/resources/logback.xml)
<configuration>
<appender name="awslambda" class="org.jlib.cloud.aws.lambda.logback.AwsLambdaAppender">
<encoder type="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] <%-36X{AWSRequestId:-request-id-not-set-by-lambda-runtime}>
%-5level %logger{10} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="awslambda" />
</root>
</configuration>
Unlike other solutions, this Logback Appender correctly handles multi-linelog messages, especially stack traces, and produces only a singleCloudWatch Logs event per message.
与其他解决方案不同,此 Logback Appender 正确处理多行日志消息,尤其是堆栈跟踪,并且每条消息仅生成一个CloudWatch Logs 事件。
The library also allows you to include the AWSRequestId, provided by the AWS Lambda runtime, in every single log message for better tracing.
该库还允许您在每条日志消息中包含由 AWS Lambda 运行时提供的 AWSRequestId,以便更好地进行跟踪。
While log4j2 requires additional handling in the build when you create an uber-jar, this solution works out of the box.
虽然在创建 uber-jar 时 log4j2 需要在构建中进行额外处理,但此解决方案开箱即用。
Disclaimer:I'm the developer of jlib
免责声明:我是jlib的开发者
回答by koppor
Just include following dependency:
只需包含以下依赖项:
<dependency>
<groupId>io.symphonia</groupId>
<artifactId>lambda-logging</artifactId>
<version>1.0.0</version>
</dependency>
Source of lambda-logging code available at https://github.com/symphoniacloud/lambda-monitoring/tree/master/lambda-logging.
可以在https://github.com/symphoniacloud/lambda-monitoring/tree/master/lambda-logging获得 lambda-logging 代码的来源。
Background information available at: https://blog.symphonia.io/a-love-letter-to-lambda-logging-974b0eb49273
背景信息可在:https: //blog.symphonia.io/a-love-letter-to-lambda-logging-974b0eb49273