Skip to content

MindSphere SDK for Java – Logging

Introduction

The MindSphere SDK for Java logging is implemented with SLF4J (Simple Logging Facade for Java) which gives access to many logging frameworks like Log4j, java.util.logging, among others allowing the end user to plug-in the desired logging framework. This topic shows how to use Log4j or Logback with SDK's logging functionality without making any change in application code.

Logging of service credentials in debug mode

If you enable SDK logging in debug mode, your service credentials will be logged. Make sure your environment is access controlled! To disable logging, see the Disable logging section.

Using Log4j for SDK logging

Download SLF4J-Log4J12 and Log4j

To use Log4j with MindSphere SDK, you need to download SLF4J-Log4J12 and Log4J jars. Build the project and ensure the jars are available as project dependencies.

Log4j uses log4j.properties as configuration file and needs to be available at the root resource folder src\main\resources.

Enable logging and setting logging levels

MindSphere SDK log levels have to be configured in log4j.properties.

The following sets the rootLogger to INFO, which causes info, warning and error messages from all loggers in the application to be included:

1
2
3
4
log4j.rootLogger=INFO, 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

Note

You can explicitly enable DEBUG, INFO or ERROR level for the MindSphere SDK Java clients by setting log level for sdk packages in log4j.properties:

log4j.logger.com.siemens.mindsphere.sdk = INFO

Disable logging

MindSphere SDK logs can be disabled by setting the logging level to OFF in log4j.properties:

1
log4j.logger.com.siemens.mindsphere.sdk = OFF

Note

If application is a spring-boot-starter project, exclude logback-classic and log4j-over-slf4j modules as follows in the dependencies section of the project's build.gradle. 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'
}

Using Logback for SDK logging

Adding logback-classic jar as a dependency

To configure MindSphere SDK log levels using logback.xml as the logging configuration file, add the logback-classic jar to project dependency. logback.xml needs to 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 setting logging levels

MindSphere SDK log levels have to be configured in logback.xml.

The following example sets the rootLogger to DEBUG, which causes info, warning and error messages from all loggers in the application to be included:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?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>

Note

You can explicitly enable DEBUG, INFO or ERROR level for the MindSphere SDK Java clients by setting log level for sdk packages in logback.xml:

1
2
#Enable DEBUG/INFO/ERROR level for the MindSphere SDK Java clients by setting log level
<logger name="com.siemens.mindsphere.sdk" level="INFO"/>

Disable logging

MindSphere SDK logs can be disabled by setting the logging level to OFF in logback.xml:

1
<logger name="com.siemens.mindsphere.sdk" level="OFF"/>

Any questions left?

Ask the community


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