Skip to content

Industrial IoT SDK for Java – Logging

Introduction

Logging of the Industrial IoT SDK for Java is implemented with SLF4J (Simple Logging Facade for Java), which gives access to many logging frameworks such as Log4j or java.util.logging. This allows the end user to plug-in the desired logging framework. This section shows how to use Log4j or Logback with the SDK's logging functionality without making any change in application code.

Attention

Do not enable SDK logging for Industrial IoT SDK for Java version 1.1.0 as it logs service credentials. If it is already enabled, disable it. To disable, refer to section Disable logging.

SDK Logging with Log4j or Logback

Preparation

Prepare Log4j for Industrial IoT SDK Logging

If you want to use Log4j for Industrial IoT SDK logging, you need to download SLF4J-Log4J12 and Log4J jars. Build the project and ensure the jars are available as project dependencies.

The configuration file log4j.properties must be available at the root resource folder src\main\resources.

Prepare Logback for Industrial IoT SDK Logging

If you want to use Logback for Industrial IoT SDK logging, you need to add the logback-classic jar to the project dependency.

The configuration file logback.xml must be available at the root resource folder src\main\resources.

Note

The logback-classic jar is implicitly available, if the application is a spring-boot-starter project.

Enable Logging and Set Log Levels

Log levels have to be configured in the configuration file of your logging framework. The Industrial IoT SDK supports the log levels DEBUG, INFO and ERROR.

The following examples illustrate how to set the rootLogger to DEBUG, which causes info, warning and error messages from all loggers in the application to be logged:

# set log level in the log4j.properties file
log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p (%t) [%c] - %m%n

<!-- set log level in the logback.xml file -->
<?xml version="1.0" encoding="UTF-8"?>

<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
      <Pattern>
        %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
      </Pattern>
    </layout>
  </appender>

  <root level="DEBUG">
    <appender-ref ref="STDOUT"/>
  </root>

</configuration>

The following examples show how to set the log level only for the Industrial IoT SDK:

#set log level in the log4j.properties file
log4j.logger.com.siemens.mindsphere.sdk = INFO
<!-- set log level in the logback.xml file -->
<logger name="com.siemens.mindsphere.sdk" level="INFO"/>

Disable logging

Industrial IoT SDK logs can be disabled by setting the logging level to OFF in the configuration file:

#disable logging in the log4j.properties file
log4j.logger.com.siemens.mindsphere.sdk = OFF
<!-- disable logging in the logback.xml file -->
<logger name="com.siemens.mindsphere.sdk" level="OFF"/>

Spring-boot-starter projects

If your application is a spring-boot-starter project, exclude the logback-classic and log4j-over-slf4j modules in the dependencies section of the project's build.gradle as shown below. This is to ensure that the log4j.properties file is considered as the logging configuration file.

// Use only if `log4j-over-slf4j` and `logback-classic` are available in project dependencies
compile ('org.springframework.boot:spring-boot-starter-web') {
    exclude module : 'log4j-over-slf4j'
    exclude module: 'logback-classic'
}

Last update: February 23, 2024

Except where otherwise noted, content on this site is licensed under the Development License Agreement.