Azure Java SDK:如何禁用控制台日志记录?
Azure Java SDK: How to disable logging to console?
我正在使用 Azure 的 Java SDK 和 Maven 开发应用程序。此应用程序将数据发送到 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 应该是发起日志记录的 class。
接下来是一些自己写入控制台的输出。
...
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
日志输出到控制台。
- 将日志级别从
FINEST
或INFO
升级到WARNING
到SEVERE
,可以参考Oracle Javadoc for class 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
我正在使用 Azure 的 Java SDK 和 Maven 开发应用程序。此应用程序将数据发送到 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 应该是发起日志记录的 class。
接下来是一些自己写入控制台的输出。
...
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
日志输出到控制台。
- 将日志级别从
FINEST
或INFO
升级到WARNING
到SEVERE
,可以参考Oracle Javadoc for classLevel
,如下,则不输出低级日志。
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