Skip to content

Commit

Permalink
Update logging.md (#1766)
Browse files Browse the repository at this point in the history
Some out-of-date documentation that didn't get updated when we upgraded to using log4j2 a while back
  • Loading branch information
timtay-microsoft authored Dec 1, 2023
1 parent 481f9bd commit 058ed2f
Showing 1 changed file with 46 additions and 26 deletions.
72 changes: 46 additions & 26 deletions logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,74 @@

The Azure IoT SDK uses the [SLF4j](http://www.slf4j.org/faq.html) logging facade. In order to capture the SDK's logs,
you will need to provide a consumer for this logging facade. As an example, the sample projects in this repo use
the library org.slf4j.slf4j-log4j12

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.29</version>
</dependency>

Alongside the above dependency, you need to add log4j2.properties files into your code's resources folder that
specify what package's logs will be captured, and at what log level. An example log4j.properties
file can be seen in a device client sample [here](device/iot-device-samples/send-event/src/main/resources/log4j2.properties)
the library org.apache.logging.log4j.log4j-slf4j-impl

```xml
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</dependency>
```

Alongside the above dependencies, you need to add log4j2.properties files into your code's resources folder that
specify what package's logs will be captured, and at what log level. An example log4j2.properties
file can be seen in a device client sample [here](iothub/device/iot-device-samples/send-event/src/main/resources/log4j2.properties)

Note that the log4j2.properties file requires you to explicitly choose which packages to collect logs from. Packages to collect
logs from must be specified in the properties file. Specified packages will include the logs
of any subpackages, but do not include the logs of their dependencies. As an example, adding the package
```com.microsoft.azure.sdk.iot.service``` will collect logs from classes like ```com.microsoft.azure.sdk.iot.service.messaging.MessagingClient```,
and will collect logs for classes like ```com.microsoft.azure.sdk.iot.service.transport.amqps.AmqpSendHandler``` but will not
collect logs from a class of its dependency, such as ```com.microsoft.azure.sdk.iot.provisioning.device.transport.amqp.ErrorLoggingBaseHandler```
collect logs from any class outside of this namespace.

Adding these packages to your log4j.properties file should be sufficient to capture all logs that this SDK generates:
Adding these packages to your log4j2.properties file should be sufficient to capture all logs that this SDK generates:
```
log4j.logger.com.microsoft.azure.sdk.iot.device = DEBUG
log4j.logger.com.microsoft.azure.sdk.iot.service = DEBUG
log4j.logger.com.microsoft.azure.sdk.iot.deps = DEBUG
log4j.logger.com.microsoft.azure.sdk.iot.provisioning.device = DEBUG
log4j.logger.com.microsoft.azure.sdk.iot.provisioning.service = DEBUG
log4j.logger.com.microsoft.azure.sdk.iot.provisioning.security = DEBUG
logger.sdk.device.name = com.microsoft.azure.sdk.iot.device
logger.sdk.device.level = INFO
logger.sdk.service.name = com.microsoft.azure.sdk.iot.service
logger.sdk.service.level = INFO
logger.sdk.provisioningDevice.name = com.microsoft.azure.sdk.iot.provisioning.device
logger.sdk.provisioningDevice.level = INFO
logger.sdk.provisioningService.name = com.microsoft.azure.sdk.iot.provisioning.service
logger.sdk.provisioningService.level = INFO
logger.sdk.provisioningService.name = com.microsoft.azure.sdk.iot.provisioning.security
logger.sdk.provisioningSecurity.level = INFO
```

Or for simplicity:
```
log4j.logger.com.microsoft.azure.sdk.iot = DEBUG
logger.sdk.name = com.microsoft.azure.sdk.iot
logger.sdk.level = INFO
```

This SDK does print logs at TRACE, DEBUG, INFO, WARN, and ERROR levels, and your log4j.properties file
allows you to choose these levels for each package. For example, to get trace level logs in the deps package,
but info level logs in the device and service packages, your log4j.properties file would include
allows you to choose these levels for each package. For example, to get trace level logs in the service client package,
but info level logs in the device client package, your log4j.properties file would include

```
log4j.logger.com.microsoft.azure.sdk.iot.device = INFO
log4j.logger.com.microsoft.azure.sdk.iot.service = INFO
log4j.logger.com.microsoft.azure.sdk.iot.deps = TRACE
logger.sdk.device.name = com.microsoft.azure.sdk.iot.device
logger.sdk.device.level = INFO
logger.sdk.service.name = com.microsoft.azure.sdk.iot.service
logger.sdk.service.level = TRACE
```

Another notable logging consumer that works with SLF4j is [logback](http://logback.qos.ch/). For more information on
using logback with SLF4j, see [this tutorial](https://mkyong.com/logging/slf4j-logback-tutorial/)

You can also make your existing logging framework compatible with SLF4j as documented [here](http://www.slf4j.org/faq.html#slf4j_compatible)


For additional details about configuring a log4j2.properties file, or to see what alternatives there are for setting these logging settings, see the official Apache documentation [here](https://logging.apache.org/log4j/2.x/manual/configuration.html).

0 comments on commit 058ed2f

Please sign in to comment.