Difference between revisions of "Logging in Simantics Platform"

From Developer Documents
Jump to navigation Jump to search
(Created page with "For uniform logging in Simantics Platform [http://www.slf4j.org/ The Simple Logging Facade for Java] (<code>org.slf4j.api</code>) and [http://logback.qos.ch/ Logback Project]...")
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
For uniform logging in Simantics Platform [http://www.slf4j.org/ The Simple Logging Facade for Java] (<code>org.slf4j.api</code>) and [http://logback.qos.ch/ Logback Project] (<code>ch.qos.logback.classic</code>) is included in Simantics SDK. To use the SLF4J logging API just include the following bundle in your own plugin's MANIFEST.MF dependencies:
+
For uniform logging in Simantics Platform [http://www.slf4j.org/ The Simple Logging Facade for Java] (<code>org.slf4j.api</code>) and [http://logback.qos.ch/ Logback Project] (<code>ch.qos.logback.classic</code>) is included in Simantics SDK. To use the SLF4J logging API just include the following bundle in your own plugin's <code>MANIFEST.MF</code> dependencies:
  
 
<pre>
 
<pre>
Line 33: Line 33:
  
 
The SLF4J Manual can be found here: http://www.slf4j.org/manual.html
 
The SLF4J Manual can be found here: http://www.slf4j.org/manual.html
 +
 +
== Configuring Logback ==
 +
 +
By default bundle <code>org.simantics.logback.configuration</code> contains the <code>logback.xml</code> configuration file in which the logging format and different appenders are defined. By default it looks like this:
 +
 +
<pre>
 +
<?xml version="1.0" encoding="UTF-8"?>
 +
<configuration>
 +
 +
  <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
 +
    <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
 +
    <encoder>
 +
      <pattern>%-5p [%d] %c: %m%n%rEx</pattern>
 +
    </encoder>
 +
  </appender>
 +
 +
  <appender name="async-console" class="ch.qos.logback.classic.AsyncAppender">
 +
    <appender-ref ref="console" />
 +
  </appender>
 +
 +
  <root level="debug">
 +
    <appender-ref ref="async-console" />
 +
  </root>
 +
</configuration>
 +
</pre>
 +
 +
This configuration creates a single ''asynchronous'' <code>ConsoleAppender</code> which prints all the logging to <code>System.out</code> by. An example output would look like:
 +
 +
<pre>
 +
INFO  [2016-09-23 15:56:25,498] org.simantics.workbench.internal.SimanticsWorkbenchAdvisor: startPlatform finished
 +
</pre>
 +
 +
It is possible to override this default configuration be giving a VM-argument for the application in format:
 +
 +
<pre>
 +
-Dlogback.configurationFile="C:\logbacs\logback.xml"
 +
</pre>
 +
 +
For detailed information see [http://logback.qos.ch/manual/ Logback manual]

Latest revision as of 13:47, 23 September 2016

For uniform logging in Simantics Platform The Simple Logging Facade for Java (org.slf4j.api) and Logback Project (ch.qos.logback.classic) is included in Simantics SDK. To use the SLF4J logging API just include the following bundle in your own plugin's MANIFEST.MF dependencies:

Require-Bundle: ..,
org.slf4j.api

An example usage of logging inside your own java code is presented below:

 1: import org.slf4j.Logger;
 2: import org.slf4j.LoggerFactory;
 3: 
 4: public class Wombat {
 5:  
 6:   private static final Logger LOGGER = LoggerFactory.getLogger(Wombat.class);
 7:   private Integer t;
 8:   private Integer oldT;
 9:
10:   public void setTemperature(Integer temperature) {
11:    
12:     oldT = t;        
13:     t = temperature;
14:
15:     LOGGER.debug("Temperature set to {}. Old temperature was {}.", t, oldT);
16:
17:     if(temperature.intValue() > 50) {
18:       LOGGER.info("Temperature has risen above 50 degrees.");
19:     }
20:   }
21: } 

The SLF4J Manual can be found here: http://www.slf4j.org/manual.html

Configuring Logback

By default bundle org.simantics.logback.configuration contains the logback.xml configuration file in which the logging format and different appenders are defined. By default it looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

  <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%-5p [%d] %c: %m%n%rEx</pattern>
    </encoder>
  </appender>

  <appender name="async-console" class="ch.qos.logback.classic.AsyncAppender">
    <appender-ref ref="console" />
  </appender>

  <root level="debug">
    <appender-ref ref="async-console" />
  </root>
</configuration>

This configuration creates a single asynchronous ConsoleAppender which prints all the logging to System.out by. An example output would look like:

INFO  [2016-09-23 15:56:25,498] org.simantics.workbench.internal.SimanticsWorkbenchAdvisor: startPlatform finished

It is possible to override this default configuration be giving a VM-argument for the application in format:

-Dlogback.configurationFile="C:\logbacs\logback.xml"

For detailed information see Logback manual