gpt4 book ai didi

java - Azure Java SDK : How to disable logging to console?

转载 作者:行者123 更新时间:2023-12-02 01:40:15 25 4
gpt4 key购买 nike

我正在使用 Azure 的 Java SDKMaven 开发一个应用程序。此应用程序将数据发送到 IoT 中心以及一些对于问题范围而言并不重要的其他功能。
我使用 log4j2 在应用程序内实现了自己的日志记录,我对此很满意,因为我可以根据需要修改和更改它。

当我检查应用程序控制台输出中出现的此警告时,出现了问题:

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

感谢this SO question我能够执行正确的操作,并在我的 pom.xml 文件中添加依赖项,如下所示:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.project.myProject</groupId>
<artifactId>myProject</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>
...
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.25</version>
</dependency>
...

添加此内容后,Azure 的 SDK 开始打印以控制台大量我不想看到的信息。 This应该是发起日志记录的类。
接下来是一些自行写入控制台的输出。

...
Jun 07, 2018 8:09:18 PM com.microsoft.azure.sdk.iot.device.CustomLogger LogInfo
INFO: IotHubConnectionString object is created successfully for iotHub.azure-devices.net, method name is <init>
Jun 07, 2018 8:09:19 PM com.microsoft.azure.sdk.iot.device.CustomLogger LogInfo
INFO: DeviceClientConfig object is created successfully with IotHubName=iotHub.azure-devices.net, deviceID=device01 , method name is <init>
Jun 07, 2018 8:09:20 PM com.microsoft.azure.sdk.iot.device.CustomLogger LogInfo
INFO: DeviceIO object is created successfully, method name is <init>
Jun 07, 2018 8:09:20 PM com.microsoft.azure.sdk.iot.device.CustomLogger LogInfo
INFO: Setting SASTokenExpiryTime as 2400 seconds, method name is setOption_SetSASTokenExpiryTime
...

我已尝试禁用Logger,但没有成功(随后this SO question)。

我想知道是否有人遇到过这个问题,如果有,我该如何禁用日志记录功能或抑制警告?
预先非常感谢!

最佳答案

有一个博客How to Configure SLF4J with Different Logger Implementations您可以引用它来配置 slf4j-jdk14 记录器实现,如下所示。

Using slf4j with JDK logger

The JDK actually comes with a logger package, and you can replace pom.xml with this logger implementation.

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.5</version>
</dependency>

Now the configuration for JDK logging is a bit difficult to work with. Not only need a config file, such assrc/main/resources/logging.properties, but you would also need to add a System properties -Djava.util.logging.config.file=logging.properties in order to have it pick it up. Here is an example to get you started:

level=INFO
handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=FINEST
deng.level=FINEST

有两种方法可以避免将这些 INFO 日志输出到控制台。

  1. 将日志级别从FINESTINFO升级到WARNINGSEVERE,可以引用Oracle类的 Javadoc Level ,如下,则不输出低级日志。

The levels in descending order are:

SEVERE (highest value)
WARNING
INFO
CONFIG
FINE
FINER
FINEST (lowest value)
  • 更改 logging.properties 中的handler 值。除了ConsoleHandler之外,您还可以使用其他四个处理程序,如下所示,请参阅java.utils.logging包摘要。
    • ConsoleHandler: This Handler publishes log records to System.err.
    • FileHandler: Simple file logging Handler.
    • MemoryHandler: Handler that buffers requests in a circular buffer in memory.
    • SocketHandler: Simple network logging Handler.
    • StreamHandler: Stream based logging Handler.

    例如将日志输出到文件

    handlers=java.util.logging.FileHandler
    java.util.logging.FileHandler.level=INFO
    java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
    java.util.logging.FileHandler.limit=1024000
    java.util.logging.FileHandler.count=10
    java.util.logging.FileHandler.pattern=logs/mylog.log
    java.util.logging.FileHandler.append=true

    关于java - Azure Java SDK : How to disable logging to console?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54554782/

    25 4 0
    文章推荐: publish-subscribe - 避免订阅者接收发布的消息(通过 Hazelcast Pub/Sub)
    文章推荐: reactjs - 下面代码中的
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com