Hexa's Blog

Talend, How to export log in JSON Format?

21/06/2023 @ Saigon Talend

Step 1: Go to File → Project Properties

[1] File → Project Properties
[1] File → Project Properties

Step 2: Go to log4j section and Activate log4j in components (Log4j version: log4j2)

[2] Activate log4j in components
[2] Activate log4j in components

In the Log4j template, use the following template.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration >
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout  >
        <pattern>
          { "timestamp":"%d{ISO8601}{GMT+0}", "level":"%level", "category":"%c", "message":"%enc{%m}{JSON}" }%n
        </pattern>
      </PatternLayout>
    </Console>
  </Appenders>

  <Loggers>
    <Root level="INFO">
      <AppenderRef ref="Console" />
    </Root>
  </Loggers>
</Configuration>

Step 3: Create tJava in Talend Job to export log

[3] tJava
[3] tJava
  • Basic settings code:
String message = "Hello World";
log.fatal(message);
log.error(message);
log.warn(message);
log.info(message);
log.debug(message);
log.trace(message);
  • Advanced settings code:
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

You then can RUN your talend job, the output should look like the following.

[4] Basic Run - Console
[4] Basic Run - Console

References