Overview

Elektron Message API (EMA Java API) allows developers integrate the EMA Java application with Apache Log4j which is a de facto standard logging framework for Java-based application at deployment time by using the Simple Logging Facade for Java (SLF4J) API as a facade for logging utility.

Even though the EMA Java API binds the logging mechanism with Java Logging API by default, developers can change the binding library and logging configuration files to bind the EMA Java aplication with Log4j or others framework that supported SLF4J without modify the application source code.

How to integrate EMA Java Application with Logging Framework

The EMA Java package contains two SLF4J library files in the <Elektron SDK Java 1.2.0 package>/Elektron-SDK-BinaryPack/Java/Ema/Libs/SLF4J/slf4j-1.7.12 folder

  • slf4j-api-1.7.12.jar

  • slf4j-jdk14-1.7.12.jar

The slf4j-api-1.7.12.jar file is the core SLF4J library file. The slf4j-jdk14-1.7.12.jar is a binding library file for Java Logging API. Developers can perform the following steps to integrate the EMA Java application log with Log4j framework.

  1. Replace this slf4j-jdk14-1.7.12.jar file with SLF4J-Log4j binding jar file

  2. Add Log4j required core library files to the Java classpath

  3. Configure configurations file to Java classpath or JVM option

Integration with Log4j 2 framework

The Log4j 2 framework requires the following libraries files in Java classpath to intergrate with EMA Java SLF4J framework:

  • log4j-api-<version>.jar
  • log4j-core-<version>.jar
  • log4j-slf4j-impl-<version>.jar
  • slf4j-api-<version>.jar

Then developers can configure Log4j 2 configurations file to Java classpath or JVM option "-Dlog4j.configurationFile" at runtime to let the EMA Java application uses Log4j 2 configurations file.

This article is based on Log4j 2 version 2.9.0 with SLF4J 1.7.12 (log4j-api-2.9.0.jar, log4j-core-2.9.0.jar and log4j-slf4j-impl-2.9.0.jar and slf4j-api-1.7.12.jar files).

Example Log4j 2 configurations file (in XML format)

The example file is saved as "\resource\log4j2.xml" file.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout pattern="current date-%d LEVEL-%-5p Thread-[%t]  Method-%M()   Class name-%C   Message-%m%n"/>
        </Console>
    </Appenders>
    <loggers>
        <Logger name="org.apache.log4j.xml" level="all"/>
        <root level="all">
            <appender-ref ref="STDOUT"/>
        </root>
    </loggers>
</Configuration>

Then you can run the EMA Java example with JVM option -Dlog4j.configurationFile points to the log4j2.xml file. You also need to include the Log4j 2 libraries files in the Java classpath too.

java -Dlog4j.configurationFile=..\resource\log4j2.xml -cp .;..\libs\ema-3.2.0.1.jar;..\libs\upa-3.2.0.1.jar;..\libs\upaValueAdd-3.2.0.1.jar;..\libs\commons-collections-3.2.2.jar;..\libs\commons-configuration-1.10.jar;..\libs\commons-lang-2.6.jar;..\libs\commons-logging-1.2.jar;..\libs\slf4j-api-1.7.12.jar;..\libs\log4j-api-2.9.0.jar;..\libs\log4j-core-2.9.0.jar;..\libs\log4j-slf4j-impl-2.9.0.jar; com.thomsonreuters.ema.examples.training.consumer.series100.example100__MarketPrice__Streaming.Consumer

An example result with Log4j 2 is shown below:

current date-2017-10-25 16:19:11,992 LEVEL-TRACE Thread-[main]  Method-log()   Class name-com.thomsonreuters.ema.access.ConfigErrorTracker   Message-loggerMsg
    ClientName: EmaConfig
    Severity: Trace
    Text:    reading configuration file [EmaConfig.xml] from [D:\Project\Code\SLF4J\emajava]
loggerMsgEnd

....

current date-2017-10-25 16:19:12,238 LEVEL-TRACE Thread-[main]  Method-<init>()   Class name-com.thomsonreuters.ema.access.CallbackClient   Message-loggerMsg
    ClientName: LoginCallbackClient
    Severity: Trace
    Text:    Created LoginCallbackClient
loggerMsgEnd


current date-2017-10-25 16:19:12,239 LEVEL-TRACE Thread-[main]  Method-initialize()   Class name-com.thomsonreuters.ema.access.LoginCallbackClient   Message-loggerMsg
    ClientName: LoginCallbackClient
    Severity: Trace
    Text:    RDMLogin request message was populated with this info: 
	LoginRequest: 
	streamId: 1
	userName: user
	streaming: true
	nameType: 1
	applicationId: 256
	applicationName: ema
	position: 10.42.68.162/U8004042-TPL-A

loggerMsgEnd

Integration with Log4j 1.x framework

The Log4j 1.x framework requires the following libraries files in Java classpath to intergrate with EMA Java SLF4J framework:

  • log4j-<version>.jar
  • slf4j-log4j12-<version>.jar
  • slf4j-api-<version>.jar

Then developers can configure Log4j 1.x configurations file to Java classpath or JVM option "-Djava.util.logging.config.file" at runtime to let the EMA Java application uses Log4j 1.x configurations file.

This article is based on Log4j version 1.2.17 with SLF4J 1.7.12 (log4j-1.2.17.jar, slf4j-log4j12-1.7.12.jar and and slf4j-api-1.7.12.jar files).

Note: Please note that the Log4j 1.x had reached end of life since August 5, 2015 by Apache Logging Services™ Project Management Committee.

Example Log4j configurations file

The example file is saved as "\resource\log4j.properties" file.

log4j.rootLogger=TRACE, STDOUT
#log4j.logger.com.tr.slf4jexample=TRACE
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

Then you can run the EMA Java example with JVM option -Dlog4j.configurationFile points to the log4j.properties file. You also need to include the Log4j libraries file in the Java classpath too.

java -Dlog4j.configurationFile=..\resource\log4j.properties -cp .;..\libs\ema-3.2.0.1.jar;..\libs\upa-3.2.0.1.jar;..\libs\upaValueAdd-3.2.0.1.jar;..\libs\commons-collections-3.2.2.jar;..\libs\commons-configuration-1.10.jar;..\libs\commons-lang-2.6.jar;..\libs\commons-logging-1.2.jar;..\libs\slf4j-api-1.7.12.jar;..\libs\slf4j-log4j12-1.7.12.jar;..\libs\log4j-1.2.17.jar; com.thomsonreuters.ema.examples.training.consumer.series100.example100__MarketPrice__Streaming.Consumer

An example result with Log4j is shown below:

TRACE [main] (ConfigErrorTracker.java:123) - loggerMsg
    ClientName: EmaConfig
    Severity: Trace
    Text:    reading configuration file [EmaConfig.xml] from [D:\Project\Code\SLF4J\emajava]
loggerMsgEnd

....

TRACE [main] (ItemCallbackClient.java:91) - loggerMsg
    ClientName: LoginCallbackClient
    Severity: Trace
    Text:    Created LoginCallbackClient
loggerMsgEnd


TRACE [main] (LoginCallbackClient.java:92) - loggerMsg
    ClientName: LoginCallbackClient
    Severity: Trace
    Text:    RDMLogin request message was populated with this info: 
	LoginRequest: 
	streamId: 1
	userName: user
	streaming: true
	nameType: 1
	applicationId: 256
	applicationName: ema
	position: 10.42.68.162/U8004042-TPL-A

loggerMsgEnd

Conclusion

The EMA Java API is implemented on top of SLF4J API as a facade for logging utility. It allows developers integrate EMA Java application with their prefer Logging framework by replacing the Logging library and configurations files without touching the application source code.

References

For further details, please check out the following resources: