禁用 Azure 服务总线在 CloudWatch 中打印日志

huangapple go评论67阅读模式
英文:

Disable Azure service bus printing logs in cloud watch

问题

在 AWS Lambda 日志中,我遇到了以下错误消息:

SLF4J: 无法加载类 "org.slf4j.impl.StaticLoggerBinder"。
SLF4J: 默认为无操作 (NOP) 日志记录器实现
SLF4J: 详细信息请参阅 http://www.slf4j.org/codes.html#StaticLoggerBinder。

因此我添加了 Maven 依赖如下:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.21</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.21</version>
</dependency>

但是在添加 slf4j 依赖之后,Azure Service Bus 的不需要的日志也开始打印,例如:

[ReactorThread1288184d-400a-4928-b174-b819c8bd9ee1] INFO com.microsoft.azure.servicebus.primitives.MessagingFactory - starting reactor instance.

我的 log4j.xml 文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration packages="com.amazonaws.services.lambda.runtime.log4j2">
    <Appenders>
        <Lambda name="Lambda">
            <PatternLayout>
                <pattern>%d{yyyy-MM-dd HH:mm:ss} %X{AWSRequestId} %-5p %c{1}:%L - %m%n</pattern>
            </PatternLayout>
        </Lambda>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Lambda" />
        </Root>
    </Loggers>
</Configuration>

如何禁止打印这些日志呢?

英文:

I was getting following error message in aws lambda logs :

SLF4J: Failed to load class &quot;org.slf4j.impl.StaticLoggerBinder&quot;.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. 

so i added maven depdenecies as :

&lt;dependency&gt;
			&lt;groupId&gt;org.slf4j&lt;/groupId&gt;
			&lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
			&lt;version&gt;1.7.21&lt;/version&gt;
		&lt;/dependency&gt;
		&lt;dependency&gt;
			&lt;groupId&gt;org.slf4j&lt;/groupId&gt;
			&lt;artifactId&gt;slf4j-simple&lt;/artifactId&gt;
			&lt;version&gt;1.7.21&lt;/version&gt;
		&lt;/dependency&gt;

But on adding slfj4j dependencies unwated logs from azure service bus are also getting printed now eg.

[ReactorThread1288184d-400a-4928-b174-b819c8bd9ee1] INFO com.microsoft.azure.servicebus.primitives.MessagingFactory - starting reactor instance.

my log4j.xml looks like this :

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;Configuration packages=&quot;com.amazonaws.services.lambda.runtime.log4j2&quot;&gt;
	&lt;Appenders&gt;
		&lt;Lambda name=&quot;Lambda&quot;&gt;
			&lt;PatternLayout&gt;
				&lt;pattern&gt;%d{yyyy-MM-dd HH:mm:ss} %X{AWSRequestId} %-5p %c{1}:%L - %m%n&lt;/pattern&gt;
			&lt;/PatternLayout&gt;
		&lt;/Lambda&gt;
	&lt;/Appenders&gt;
	&lt;Loggers&gt;
		&lt;Root level=&quot;info&quot;&gt;
			&lt;AppenderRef ref=&quot;Lambda&quot; /&gt;
		&lt;/Root&gt;
	&lt;/Loggers&gt;
&lt;/Configuration&gt;

How can i disable these logs getting printed ?

答案1

得分: 1

以下是翻译好的部分:

我认为问题与您项目的日志依赖关系有关。

一方面,您在AWS Lambda上使用了Log4j2,它使用Log4j2进行日志记录。另一方面,您有Azure Service Bus,它使用SLF4J API门面进行日志记录。您需要配置系统以支持这两种日志记录方法。

首先,您需要在pom.xml文件中添加以下依赖项:

&lt;dependencies&gt;
  ...
  &lt;dependency&gt;
    &lt;groupId&gt;com.amazonaws&lt;/groupId&gt;
    &lt;artifactId&gt;aws-lambda-java-log4j2&lt;/artifactId&gt;
    &lt;version&gt;1.2.0&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
    &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
    &lt;artifactId&gt;log4j-slf4j-impl&lt;/artifactId&gt;
    &lt;version&gt;2.13.2&lt;/version&gt;
  &lt;/dependency&gt;
  ....
&lt;/dependencies&gt;

第一个依赖项允许您将日志跟踪发送到AWS Lambda。

最后一个依赖项是Log4j2 SLF4J Java桥,用于Azure Service Bus日志记录。

另外,请删除与组org.slf4j相关联的依赖项slf4j-simple,它将不再需要。

在安装了这些依赖项之后,请在XML配置文件中包含以下行:

&lt;Logger name=&quot;com.microsoft.azure.servicebus&quot; level=&quot;OFF&quot;/&gt;

在您的XML配置文件中(按照惯例,最好将Log4j2配置文件命名为log4j2.xml,而不是log4j.xml,更适用于旧的Log4j库版本)。

您的log4j2.xml应该类似于以下内容:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;Configuration packages=&quot;com.amazonaws.services.lambda.runtime.log4j2&quot;&gt;
    &lt;Appenders&gt;
        &lt;Lambda name=&quot;Lambda&quot;&gt;
            &lt;PatternLayout&gt;
                &lt;pattern&gt;%d{yyyy-MM-dd HH:mm:ss} %X{AWSRequestId} %-5p %c{1}:%L - %m%n&lt;/pattern&gt;
            &lt;/PatternLayout&gt;
        &lt;/Lambda&gt;
    &lt;/Appenders&gt;
    &lt;Loggers&gt;
        &lt;Logger name=&quot;com.microsoft.azure.servicebus&quot; level=&quot;OFF&quot;/&gt;
        &lt;Root level=&quot;info&quot;&gt;
            &lt;AppenderRef ref=&quot;Lambda&quot; /&gt;
        &lt;/Root&gt;
    &lt;/Loggers&gt;
&lt;/Configuration&gt;
英文:

I think the problem is related with the logging dependencies of your project.

On one hand, you have AWS Lambda over Log4j2, which uses Log4j2 for logging. On the other hand you have Azure Service Bus, which uses the SLF4J API facade for logging. You need to configure your system to support both logging approaches.

First of all, you need the following dependencies in your pom.xml file:

&lt;dependencies&gt;
  ...
  &lt;dependency&gt;
    &lt;groupId&gt;com.amazonaws&lt;/groupId&gt;
    &lt;artifactId&gt;aws-lambda-java-log4j2&lt;/artifactId&gt;
    &lt;version&gt;1.2.0&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
    &lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
    &lt;artifactId&gt;log4j-slf4j-impl&lt;/artifactId&gt;
    &lt;version&gt;2.13.2&lt;/version&gt;
  &lt;/dependency&gt;
  ....
&lt;/dependencies&gt;

The first dependency allows you to emit log traces to AWS Lambda.

The last one is the Log4j2 SLF4J java bridge, necessary for Azure Service Bus logging.

Please, also remove the dependency slf4j-simple associated with the group org.slf4j, it will be no necessary.

With this dependencies in place, please, include the following line:

&lt;Logger name=&quot;com.microsoft.azure.servicebus&quot; level=&quot;OFF&quot;/&gt;

In your XML configuration file (by convention, it is better to name your Log4j2 configuration file like log4j2.xml instead of log4j.xml, more appropriate for the old Log4j library version).

Your log4j2.xml should looks something like the following:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;Configuration packages=&quot;com.amazonaws.services.lambda.runtime.log4j2&quot;&gt;
    &lt;Appenders&gt;
        &lt;Lambda name=&quot;Lambda&quot;&gt;
            &lt;PatternLayout&gt;
                &lt;pattern&gt;%d{yyyy-MM-dd HH:mm:ss} %X{AWSRequestId} %-5p %c{1}:%L - %m%n&lt;/pattern&gt;
            &lt;/PatternLayout&gt;
        &lt;/Lambda&gt;
    &lt;/Appenders&gt;
    &lt;Loggers&gt;
        &lt;Logger name=&quot;com.microsoft.azure.servicebus&quot; level=&quot;OFF&quot;/&gt;
        &lt;Root level=&quot;info&quot;&gt;
            &lt;AppenderRef ref=&quot;Lambda&quot; /&gt;
        &lt;/Root&gt;
    &lt;/Loggers&gt;
&lt;/Configuration&gt;

答案2

得分: 0

首先,这不是一个错误。这条消息表示类路径中没有日志记录器实现,因此它默认为 NOP(无操作)日志记录。由于 SLF4J 是不同日志记录框架的抽象层,除了只有 SLF4J,您还需要在类路径中包含特定的日志记录框架。由于您正在尝试使用 Lambda appender,您需要将 aws-lambda-java-log4j2 添加到类路径中。这将同时引入所需的 SLF4J 依赖。

截至 2020 年 5 月 5 日的最新版本:

&lt;dependency&gt;
    &lt;groupId&gt;com.amazonaws&lt;/groupId&gt;
    &lt;artifactId&gt;aws-lambda-java-log4j2&lt;/artifactId&gt;
    &lt;version&gt;1.2.0&lt;/version&gt;
&lt;/dependency&gt;
英文:

Firstly, this is not an error. The message means that there is no logger implementation present in the classpath and hence it is defaulting to NOP (No-operation) log. And as SLF4J is an abstraction of different logging frameworks, you also need to include specific logging framework in your classpath apart from only having SLF4J. Since you are trying to use Lambda appender, you need to add aws-lambda-java-log4j2 to your classpath. This will bring in required SLF4J dependencies also.

Latest version as of May 05, 2020:

&lt;dependency&gt;
    &lt;groupId&gt;com.amazonaws&lt;/groupId&gt;
    &lt;artifactId&gt;aws-lambda-java-log4j2&lt;/artifactId&gt;
    &lt;version&gt;1.2.0&lt;/version&gt;
&lt;/dependency&gt;

huangapple
  • 本文由 发表于 2020年9月25日 01:40:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64051659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定