java – 如何重定向AWS sdk日志输出

前端之家收集整理的这篇文章主要介绍了java – 如何重定向AWS sdk日志输出前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
即使我正在使用logback并进行配置,我也不断得到STDOUT.我无法将AWS的东西从控制台中取出.
Jun 19,2014 3:46:40 PM com.amazonaws.http.AmazonHttpClient executeHelper
INFO: Unable to execute HTTP request: The target server Failed to respond
org.apache.http.NoHttpResponseException: The target server Failed to respond
        at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:95)
        at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:62)
        at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:254)
        at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:289)
        at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:252)  
        at org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader(ManagedClientConnectionImpl.java:191)   
        at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:300)
        at com.amazonaws.http.protocol.SdkHttpRequestExecutor.doReceiveResponse(SdkHttpRequestExecutor.java:66)
        at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:127)
        at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:713)
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:518)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
        at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:402)
        at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:245)
        at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:3573)
        at com.amazonaws.services.s3.AmazonS3Client.getObjectMetadata(AmazonS3Client.java:990)
        at com.amazonaws.services.s3.AmazonS3Client.getObjectMetadata(AmazonS3Client.java:970)
        at com.here.prime.cdtfilter.S3MapStore$$anonfun$1.apply(S3MapStore.scala:49)
        at com.here.prime.cdtfilter.S3MapStore$$anonfun$1.apply(S3MapStore.scala:48)
        at com.here.prime.utils.Utils$.retry(Utils.scala:26)

这是我的logback配置:

<configuration debug="false" scan="true" scanPeriod="30 seconds">
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>cdtxfilter.log</file>
        <append>true</append>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="com.amazonaws.request" level="WARN">
    </logger>

    <root level="DEBUG">
        <!--<appender-ref ref="STDOUT" />-->
        <appender-ref ref="FILE" />
    </root>
</configuration>

解:

通过logback强制登录,而不是commons-logging:

在build.sbt中添加

resolvers ++= Seq(
  "version99 Empty loggers" at "http://version99.qos.ch",)

libraryDependencies ++= Seq(
  "org.slf4j" % "jcl-over-slf4j" % "1.7.7","commons-logging" % "commons-logging" % "99-empty","ch.qos.logback" % "logback-classic" % "1.0.13",)

在logback.xml中,微调AWS SDK中嘈杂类的日志级别:

<configuration...
[..]
    <logger name="com.amazonaws" level="ERROR"/>
    <logger name="org.apache.http" level="INFO" />
</configuration>

解决方法

首先我想说的是,由于历史原因,登录java是一团糟,你应该准备好做出正确的努力.

现在,你的问题.

首先,它真的好像是您在STDOUT绕过回溯中的日志(至少它们不遵循您的对数配置中定义的任何模式).

可能有两个原因:

>您以某种不太可能的方式错误地配置了Logback(通过检查您配置的logback文件中是否有任何日志条目写入它们来验证).
>由AWS sdk处理的日志(由AWS sdk使用的日志记录系统很可能),或者是将日志记录重定向到例如SLF4J的日志(很可能,因为这需要一些显式配置类路径).

现在来处理这个我建议你从this article开始,以了解java的日志记录动物园.

接下来,为了解决您的问题,我建议您配置常用的日志记录系统(commons logging,log4j等),将其日志引导至SLF4j,并将logback用作SLF4j实现.

阅读thisthis(如果您使用maven,否则找到一种方法来排除与您的构建系统相关的jcl和log4j),以了解应该什么和什么不应放置到您的类路径中.

我的一个项目使用AWS sdk,并按上述建议设置了日志记录.

在我的pom.xml中,我设置了这样的依赖关系:

...

<repositories>
    <repository>
        <id>version99</id>
        <url>http://version99.qos.ch/</url>
    </repository>
</repositories>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>99-empty</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>99-empty</version>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.7</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>log4j-over-slf4j</artifactId>
        <version>1.7.7</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>1.7.7</version>
    </dependency>

    ...

</dependencies>

...

…并且在我的类路径我有这样的logback.xml:

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>logs.log</file>
    <append>false</append>
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
</appender>

<root level="debug">
    <appender-ref ref="FILE" />
</root>

…和所有日志最后写入我的日志文件.

这应该给你一些关于如何配置依赖/日志记录的想法.

随时澄清评论中的不清楚的时刻.

猜你在找的Java相关文章