Talend, How to export log in JSON Format?
21/06/2023
@ Saigon
Talend
Step 1: Go to File → Project Properties
![[1] File → Project Properties](/image/posts/2023-06-21-Talend-How-to-export-log-in-JSON-Format/1.png)
Step 2: Go to log4j section and Activate log4j in components (Log4j version: log4j2)
![[2] Activate log4j in components](/image/posts/2023-06-21-Talend-How-to-export-log-in-JSON-Format/2.png)
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](/image/posts/2023-06-21-Talend-How-to-export-log-in-JSON-Format/3.png)
- 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](/image/posts/2023-06-21-Talend-How-to-export-log-in-JSON-Format/4.png)
References
- Configure PatternLayout, jmcollin92, 2021, May 24, https://stackoverflow.com/questions/29387007/does-log4j-support-json-format#comment119606534_37455869.
- Handle special character in json log message, Nishan B, 2022, November 25, https://stackoverflow.com/questions/29387007/does-log4j-support-json-format#comment131629876_37455869.