Skip to main content

An example of running a Microservice Route using external Log4j configuration

Availability-noteDeprecated

This section will show you an example of how to run a Microservice Route using external Log4j configuration.

The Microservice is built from a simple Route containing a cTimer and cLog component.

Route example.

The cTimer component will trigger the message exchange five times.

Basic settings view of the cTimer component.

The cLog component will log the message exchanges. The logging level is set to WARN, which is the default configuration of Talend Studio.

Basic settings view of the cLog component.

When running the Route in Talend Studio, the following result is shown in the execution console.

Basic run view.

After building the Route to the Microservice .jar file, you can run it in a console window:

  • To run the Microservice with the default configuration, use the following command:
    java -jar demo_0.1.jar

    The following log messages are shown, which is the same as the execution result in Talend Studio.

    Log messages in the command line.
  • To run the Microservice with a custom Log4j configuration, you can use the spring.config.additional-location property to call the customized log4j.xml file in an external configuration folder. A sample Log4j configuration which is located in the config folder in the directory of the Microservice jar is shown below to provide a tesb.log file when the Microservice is started.
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
    <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
        <appender name="stdout" class="org.apache.log4j.ConsoleAppender">
            <layout class="org.apache.log4j.PatternLayout">
                <param name="ConversionPattern" value="%d | %-5.5p | %-16.16t | %-32.32C %4L | %m%n"/>
            </layout>
        </appender>
        <appender name="rollingFile" class="org.apache.log4j.RollingFileAppender">
            <param name="file" value="tesb.log"/>
            <param name="MaxFileSize" value="1024KB"/>
            <param name="MaxBackupIndex" value="10"/>
            <layout class="org.apache.log4j.PatternLayout">
                <param name="ConversionPattern" value="%d | %-5.5p | %-16.16t | %-32.32C %4L | %m%n"/>
            </layout>
        </appender>
        <root>
            <priority value="info"/>
            <appender-ref ref="stdout"/>
            <appender-ref ref="rollingFile"/>
        </root>
    </log4j:configuration>

    To use this configuration, run the following command:

    java -jar demo_0.1.jar --spring.config.additional-location=./config

    Alternatively, you can use the logging.config property to call the Log4j configuration file:

    java -jar demo_0.1.jar --logging.config=./config/log4j.xml

    The following log messages are shown, and written to the tesb.log file in the directory where the Microservice jar file is located.

    Log messages in the command line.

    The tesb.log file is created:

    The tesb.log file is located in Computer > New Volume (F:) .

    The log messages are written to the tesb.log file:

    Content of the tesb.log file.

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!