简体   繁体   English

在 AWS Lambas 中使用 slf4j(和 io.symphonia:lambda-logging)记录时 CloudWatch 输出中的新行问题

[英]New line problem in CloudWatch output when logging with slf4j (and io.symphonia:lambda-logging) in AWS Lambas

I have the problem, that when logging with slf4j (and io.symphonia:lambda-logging) in Java, for every new line in the logging message CloudWatch outputs a new log message.我遇到了问题,当在 Java 中使用 slf4j(和 io.symphonia:lambda-logging)进行日志记录时,对于日志消息中的每一行,CloudWatch 都会输出一条新的日志消息。 This also happens for exceptions using the LOGGER.error(String msg, Throwable t) )对于使用LOGGER.error(String msg, Throwable t)的异常也会发生这种情况)

And since the messages output by CloudWatch are unsorted and multiple messages can come in from different Lambdas (or other Services, etc.) the logs are becoming unreadable.由于 CloudWatch 输出的消息未排序,并且多条消息可能来自不同的 Lambda(或其他服务等),因此日志变得不可读。

One solution to this is to setup the logging pattern in the logging configuration as follows:一种解决方案是在日志记录配置中设置日志记录模式,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<configuration packages="com.amazonaws.services.lambda.runtime.log4j2">
  <appender name="Lambda" class="io.symphonia.lambda.logging.DefaultConsoleAppender">
    <encoder>
      <pattern>%date{yyyy-MM-dd HH:mm:ss} %-5level - %logger{0}:%line: %replace(%msg){'\n','&#xd;'} %replace(%exception){'\n','&#xd;'} %nopexception %n</pattern>
    </encoder>
  </appender>

  <root level="info">
    <appender-ref ref="Lambda" />
  </root>
</configuration>

See the <pattern> tag: The magic is in the %replace(%msg){'\n','&#xd;'} and the %replace(%exception){'\n','&#xd;'} %nopexception .请参阅<pattern>标记:神奇之处在于%replace(%msg){'\n','&#xd;'}%replace(%exception){'\n','&#xd;'} %nopexception Both calls are replacing the new line (\n) with a carriage return ( ) for the log message (%msg) and a passed in exception (%exception).这两个调用都将新行 (\n) 替换为日志消息 (%msg) 的回车 () 和传入的异常 (%exception)。 The reason why the second argument is in Unicode Hex Character Code and the first isn't is described in the conversation on GitHub linked below.第二个参数是 Unicode 十六进制字符代码而第一个参数不是的原因在下面链接的 GitHub 上的对话中有所描述。 I put this into a loggerconfig.xml and setup the logger to use it in the class that is the entry point for the lambda like this:我将其放入 loggerconfig.xml 并设置记录器以在作为 lambda 入口点的类中使用它,如下所示:

private static final Logger LOGGER;

  static {
    // must be set before the very first call to LoggerFactory.getLogger()
    System.setProperty(ContextInitializer.CONFIG_FILE_PROPERTY, "loggerconfig.xml");
    LOGGER = LoggerFactory.getLogger(ClassThatShallLog.class);
  }

I came to this solution through this conversation on GitHub.我通过 GitHub 上的对话找到了这个解决方案。

Use multi_line_start_pattern http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/AgentReference.html .使用 multi_line_start_pattern http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/AgentReference.html You can set it to be your timestamp, in which case you will get the desired behavior:您可以将其设置为您的时间戳,在这种情况下您将获得所需的行为:

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

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