本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用SDK适用于 Java 2.x 的
用 Amazon SDK for Java 2.x 途 SLF4J
支持的日志记录系统包括 Java Logging Framework、Apache Log4j 2
Log4j 2 配置文件
您通常是将一个名为 log4j2.xml
的配置文件与 Log4j 2 结合使用。配置文件示例如下所示。要了解有关配置文件中使用的值的更多信息,请参阅 Log4j 配置手册
应用程序启动时,该 log4j2.xml
文件必须位于类路径中。对于 Maven 项目,请将文件放在 <project-dir>/src/main/resources
目录中。
log4j2.xml
配置文件会指定日志记录级别software.amazon.awssdk
。
添加日志记录依赖项
要在生成文件SLF4J中配置 Log4j 2 绑定,请使用以下命令。
对于 log4j-slf4j2-impl
构件的最低版本,请使用 2.20.0
。要获取最新版本,请使用发布到 Maven centralVERSION
以及你将要使用的版本。
SDK-特定的错误和警告
我们建议您始终将 “software.amazon.awssdk” 记录器层次结构设置为 “”,以便从的WARN客户端库中捕获任何重要消息。SDK例如,如果 Amazon S3 客户端检测到应用程序没有正确关闭 InputStream
而且可能会泄漏资源,那么 S3 客户端将通过向日志中记录警告消息来进行报告。另外,由此可确保客户端在处理请求或响应遇到任何问题时会记录相应消息。
以下log4j2.xml
文件将设置WARN为 “”,这会导致输出来自应用程序中所有记录器的警告和错误级别消息,包括 “software.amazon.awssdk” 层次结构中的记录器。rootLogger
或者,如果使用 “software.amazon.awssdk” 记录器层次结构,则可以显式设置为 “”。WARN <Root level="ERROR">
Log4j2.xml 配置文件示例
此配置会将所有记录器层次结构的 “ERRORWARN” 和 “” 级别的消息记录到控制台。
<Configuration status="WARN"> <Appenders> <Console name="ConsoleAppender" target="SYSTEM_OUT"> <PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c:%L - %m%n" /> </Console> </Appenders> <Loggers> <Root level="WARN"> <AppenderRef ref="ConsoleAppender"/> </Root> </Loggers> </Configuration>
请求/响应摘要日志记录
对的每个请求都会 Amazon Web Services 服务 生成一个唯一的 Amazon 请求 ID,如果您在如何处理请求时遇到问题, Amazon Web Services 服务 这会很有用。 Amazon
SDK对于任何失败的服务调用,都可以通过中的SdkServiceException
以下 log4j2.xml
文件将启用请求和响应的摘要。
<Configuration status="WARN"> <Appenders> <Console name="ConsoleAppender" target="SYSTEM_OUT"> <PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c:%L - %m%n" /> </Console> </Appenders> <Loggers> <Root level="ERROR"> <AppenderRef ref="ConsoleAppender"/> </Root> <Logger name="software.amazon.awssdk" level="WARN" /> <Logger name="software.amazon.awssdk.request" level="DEBUG" /> </Loggers> </Configuration>
以下是日志输出的示例:
2022-09-23 16:02:08 [main] DEBUG software.amazon.awssdk.request:85 - Sending Request: DefaultSdkHttpFullRequest(httpMethod=POST, protocol=https, host=dynamodb.us-east-1.amazonaws.com, encodedPath=/, headers=[amz-sdk-invocation-id, Content-Length, Content-Type, User-Agent, X-Amz-Target], queryParameters=[]) 2022-09-23 16:02:08 [main] DEBUG software.amazon.awssdk.request:85 - Received successful response: 200, Request ID: QS9DUMME2NHEDH8TGT9N5V53OJVV4KQNSO5AEMVJF66Q9ASUAAJG, Extended Request ID: not available
如果您只对请求编号感兴趣,请使用 <Logger
name="software.amazon.awssdk.requestId" level="DEBUG" />
。
调试级日志 SDK
如果您需要有关正在执行的操作的SDK更多详细信息,可以将记录器的日志级别设置为。software.amazon.awssdk
DEBUG
在此级别上,它们会SDK输出大量细节,因此我们建议您设置此级别以使用集成测试来解决错误。
在此日志级别,记录有关配置、凭据解析、执行拦截器、高级TLS活动、请求签名等的信息。SDK
以下是 a SDK t DEBUG
级别为S3Client#listBuckets()
呼叫输出的语句示例。
DEBUG s.a.a.r.p.AwsRegionProviderChain:57 - Unable to load region from software.amazon.awssdk.regions.providers.SystemSettingsRegionProvider@324dcd31:Unable to load region from system settings. Region must be specified either via environment variable (AWS_REGION) or system property (aws.region). DEBUG s.a.a.c.i.h.l.ClasspathSdkHttpServiceProvider:85 - The HTTP implementation loaded is software.amazon.awssdk.http.apache.ApacheSdkHttpService@a23a01d DEBUG s.a.a.c.i.ExecutionInterceptorChain:85 - Creating an interceptor chain that will apply interceptors in the following order: [software.amazon.awssdk.core.internal.interceptor.HttpChecksumValidationInterceptor@69b2f8e5, software.amazon.awssdk.awscore.interceptor.HelpfulUnknownHostExceptionInterceptor@6331250e, software.amazon.awssdk.awscore.eventstream.EventStreamInitialRequestInterceptor@a10c1b5, software.amazon.awssdk.awscore.interceptor.TraceIdExecutionInterceptor@644abb8f, software.amazon.awssdk.services.s3.auth.scheme.internal.S3AuthSchemeInterceptor@1a411233, software.amazon.awssdk.services.s3.endpoints.internal.S3ResolveEndpointInterceptor@70325d20, software.amazon.awssdk.services.s3.endpoints.internal.S3RequestSetEndpointInterceptor@7c2327fa, software.amazon.awssdk.services.s3.internal.handlers.StreamingRequestInterceptor@4d847d32, software.amazon.awssdk.services.s3.internal.handlers.CreateBucketInterceptor@5f462e3b, software.amazon.awssdk.services.s3.internal.handlers.CreateMultipartUploadRequestInterceptor@3d7fa3ae, software.amazon.awssdk.services.s3.internal.handlers.DecodeUrlEncodedResponseInterceptor@58065f0c, software.amazon.awssdk.services.s3.internal.handlers.GetBucketPolicyInterceptor@3605c4d3, software.amazon.awssdk.services.s3.internal.handlers.S3ExpressChecksumInterceptor@585c13de, software.amazon.awssdk.services.s3.internal.handlers.AsyncChecksumValidationInterceptor@187eb9a8, software.amazon.awssdk.services.s3.internal.handlers.SyncChecksumValidationInterceptor@726a6b94, software.amazon.awssdk.services.s3.internal.handlers.EnableTrailingChecksumInterceptor@6ad11a56, software.amazon.awssdk.services.s3.internal.handlers.ExceptionTranslationInterceptor@522b2631, software.amazon.awssdk.services.s3.internal.handlers.GetObjectInterceptor@3ff57625, software.amazon.awssdk.services.s3.internal.handlers.CopySourceInterceptor@1ee29c84, software.amazon.awssdk.services.s3.internal.handlers.ObjectMetadataInterceptor@7c8326a4] DEBUG s.a.a.u.c.CachedSupplier:85 - (SsoOidcTokenProvider()) Cached value is stale and will be refreshed. ... DEBUG s.a.a.c.i.ExecutionInterceptorChain:85 - Creating an interceptor chain that will apply interceptors in the following order: [software.amazon.awssdk.core.internal.interceptor.HttpChecksumValidationInterceptor@51351f28, software.amazon.awssdk.awscore.interceptor.HelpfulUnknownHostExceptionInterceptor@21618fa7, software.amazon.awssdk.awscore.eventstream.EventStreamInitialRequestInterceptor@15f2eda3, software.amazon.awssdk.awscore.interceptor.TraceIdExecutionInterceptor@34cf294c, software.amazon.awssdk.services.sso.auth.scheme.internal.SsoAuthSchemeInterceptor@4d7aaca2, software.amazon.awssdk.services.sso.endpoints.internal.SsoResolveEndpointInterceptor@604b1e1d, software.amazon.awssdk.services.sso.endpoints.internal.SsoRequestSetEndpointInterceptor@62566842] ... DEBUG s.a.a.request:85 - Sending Request: DefaultSdkHttpFullRequest(httpMethod=GET, protocol=https, host=portal.sso.us-east-1.amazonaws.com, encodedPath=/federation/credentials, headers=[amz-sdk-invocation-id, User-Agent, x-amz-sso_bearer_token], queryParameters=[role_name, account_id]) DEBUG s.a.a.c.i.h.p.s.SigningStage:85 - Using SelectedAuthScheme: smithy.api#noAuth DEBUG s.a.a.h.a.i.c.SdkTlsSocketFactory:366 - Connecting socket to portal.sso.us-east-1.amazonaws.com/18.235.195.183:443 with timeout 2000 ... DEBUG s.a.a.requestId:85 - Received successful response: 200, Request ID: bb4f40f4-e920-4b5c-8648-58f26e7e08cd, Extended Request ID: not available DEBUG s.a.a.request:85 - Received successful response: 200, Request ID: bb4f40f4-e920-4b5c-8648-58f26e7e08cd, Extended Request ID: not available DEBUG s.a.a.u.c.CachedSupplier:85 - (software.amazon.awssdk.services.sso.auth.SsoCredentialsProvider@b965857) Successfully refreshed cached value. Next Prefetch Time: 2024-04-25T22:03:10.097Z. Next Stale Time: 2024-04-25T22:05:30Z DEBUG s.a.a.c.i.ExecutionInterceptorChain:85 - Interceptor 'software.amazon.awssdk.services.s3.endpoints.internal.S3RequestSetEndpointInterceptor@7c2327fa' modified the message with its modifyHttpRequest method. ... DEBUG s.a.a.c.i.h.p.s.SigningStage:85 - Using SelectedAuthScheme: aws.auth#sigv4 ... DEBUG s.a.a.a.s.Aws4Signer:85 - AWS4 Canonical Request: GET ... DEBUG s.a.a.h.a.a.i.s.DefaultV4RequestSigner:85 - AWS4 String to sign: AWS4-HMAC-SHA256 20240425T210631Z 20240425/us-east-1/s3/aws4_request aafb7784627fa7a49584256cb746279751c48c2076f813259ef767ecce304d64 DEBUG s.a.a.h.a.i.c.SdkTlsSocketFactory:366 - Connecting socket to s3.us-east-1.amazonaws.com/52.217.41.86:443 with timeout 2000 ...
以下log4j2.xml
文件配置了之前的输出。
<Configuration status="WARN"> <Appenders> <Console name="ConsoleAppender" target="SYSTEM_OUT"> <PatternLayout pattern="%-5p %c{1.}:%L - %m%n" /> </Console> </Appenders> <Loggers> <Root level="WARN"> <AppenderRef ref="ConsoleAppender"/> </Root> <Logger name="software.amazon.awssdk" level="DEBUG" /> </Loggers> </Configuration>
启用线路记录
查看 for Java 2.x 发送和接收SDK的确切请求和响应可能很有用。如果您需要访问这些信息,则可以根据服务客户端使用的HTTP客户端添加必要的配置来临时将其启用。
默认情况下,同步服务客户端(例如 S3Client
以下是可用于这两类服务HTTP客户端的客户端明细:
同步HTTP客户端 | 异步HTTP客户端 |
---|---|
ApacheHttpClient |
NettyNioAsyncHttpClient |
UrlConnectionHttpClient |
AwsCrtAsyncHttpClient |
AwsCrtHttpClient |
请查阅下面的相应选项卡,了解需要根据底层HTTP客户端添加的配置设置。
警告
我们建议只出于调试目的使用线路日志记录。由于线路日志记录可能记录敏感数据,因此应在您的生产环境中禁用它。它无需加密即可记录完整的请求或响应,即使是呼HTTPS叫也是如此。对于大型请求(例如,将文件上传到 Amazon S3)或响应,详细的线路记录也会显著影响应用程序的性能。