Azure Java SDK: How to disable logging to console?

孤人 提交于 2020-08-09 08:14:13

问题


I'm developing an application using Azure's Java SDK and Maven. This application sends data to an IoT Hub and some other functionalities that are not important for the scope of the question.
I implemented my own logging inside the application by using log4j2 and I'm fine with that since I can modify and change it however I want.

The problem arose when I checked this warning that was coming up in my application's console output:

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.

Thanks to this SO question I was able to do the correct move and add the dependency inside my pom.xml file like so:

<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>
...

After this addition the Azure's SDK started printing to console a lot of information that I don't really want to see. This should be the class that originates the logging.
Following, some output that gets written to console by itself.

...
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
...

I've already tried to disable the Logger but with no success (followed this SO question).

I would like to know if someone has ever had this problem and if so how can I disable the logging features or else suppress the warning?
Thanks a lot in advance!


回答1:


There is a blog How to Configure SLF4J with Different Logger Implementations which you can refer to to configure your slf4j-jdk14 logger implementation, as below.

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

There are two ways to avoid outputing these INFO logs to console.

  1. Upgrade the logging level from FINEST or INFO to WARNING to SEVERE, you can refer to the Oracle Javadoc for class Level, as below, then to not output low level logs.

The levels in descending order are:

SEVERE (highest value)
WARNING
INFO
CONFIG
FINE
FINER
FINEST (lowest value)
  1. To change the handler value in logging.properties. Except ConsoleHandler, there are four other handlers which you can use, as below, please see java.utils.logging package summary.
  • 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.

For example, to output logs to a file

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


来源:https://stackoverflow.com/questions/54554782/azure-java-sdk-how-to-disable-logging-to-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!