Skip to content

MindSphere SDK for Java – Logging

Introduction

Logging of the MindSphere 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 MindSphere 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 MindSphere SDK Logging

If you want to use Log4j for MindSphere 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 MindSphere SDK Logging

If you want to use Logback for MindSphere 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 MindSphere 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:

1
2
3
4
5
# 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
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<!-- 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 MindSphere SDK:

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

Disable logging

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

1
2
#disable logging in the log4j.properties file
log4j.logger.com.siemens.mindsphere.sdk = OFF
1
2
<!-- 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.

1
2
3
4
5
// 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'
}

Any questions left?

Ask the community


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