简体   繁体   English

使用 log4j2 在 AWS 日志中记录的垃圾值

[英]Garbage value getting logged in AWS logs using log4j2

My log4j2.xml file:我的 log4j2.xml 文件:

<Configuration status="debug">

<appenders>
    <Console name="LogToConsole">
        <PatternLayout disableAnsi="false" pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %X{api-interaction-id} %style{%C{1.}}{bright,yellow} : %msg%n%throwable" />
    </Console>
    
    <RollingFile>
        <name>infoLogs</name>
        <fileName>/usr/${logPath}/info_log.log</fileName>
        <filePattern>/usr/${logPath}/$${date:yyyy-MM}/info-%d{MM-dd-yyyy}-%i.log.gz</filePattern>
        <PatternLayout pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %X{api-interaction-id} %style{%c{1.}}{bright,yellow} : %msg%n%throwable" />
        <Policies>
            <TimeBasedTriggeringPolicy/>
            <SizeBasedTriggeringPolicy size="50MB" />
        </Policies>
        <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY" />
    </RollingFile>
</appenders>


<Loggers>
    <Logger name="com.demo" level="debug" additivity="false">
        
        <AppenderRef ref="infoLogs" level="debug"/>
        <AppenderRef ref="LogToConsole" level="debug" />
    </Logger>

    <Root level="debug">
        <AppenderRef ref="LogToConsole" level="debug" />
    </Root>
</Loggers>

</Configuration>

It is being logged properly in local location file like:它被正确记录在本地位置文件中,例如:

  2020-07-08T03:08:10,816 INFO  [main]  c.f.s.w.Application : Started Application in 131.144 seconds (JVM running for 141.392)

But in AWS log it is somehow with garbage values being added:但在 AWS 日志中,它以某种方式添加了垃圾值:

   [30m2020-07-09T06:55:16,979[m [32mINFO [m[[1;34mhttp-nio-8080-exec-6[m] [1;33mc.f.c.f.l.LoggerSlf4jImpl[m : Input Request Start:58b2d544-ca8a-45b0-9797-f5d4b79db0b8

Need solution for this?需要解决方案吗?

When you configure the output with things like highlight and style , Log4J will add ANSI escape sequences to the output.当您使用highlightstyle配置 output 时,Log4J 会将 ANSI 转义序列添加到 output。 I assume the example in AWS is from CloudWatch, where the web console view does not support rendering ANSI styling.我假设 AWS 中的示例来自 CloudWatch,其中 web 控制台视图不支持呈现 ANSI 样式。

There are a few browser plugins that will pick up the escape sequences and render them correctly.有一些浏览器插件可以获取转义序列并正确呈现它们。 I haven't tried them, so I can't say how well they work.我没有尝试过它们,所以我不能说它们的效果如何。

Your other option, which I recommend, is to turn of ANSI coloring in the logs you ship to AWS.我建议您的另一个选择是在您发送到 AWS 的日志中启用 ANSI 着色。 Even if you find a log viewer that supports ANSI, the presence of these escape sequences will make it difficult to analyse the logs (eg using CloudWatch Insights or Elasticsearch) should you want that in the future.即使您找到支持 ANSI 的日志查看器,这些转义序列的存在也会使您在将来需要时难以分析日志(例如使用 CloudWatch Insights 或 Elasticsearch)。 If it is the console appender you use for AWS, set disableAnsi to true .如果它是您用于 AWS 的控制台附加程序,请将disableAnsi设置为true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM