Author : Girish Mallula
Introduction
When your are working with larger jobs it is difficult to write all the logs to console as more and more logs write to console effects the performance of the Talend Studio. So it is best practice to limit number of lines on console but we have better way of dealing this problem.
Apache Log4j
Apache Log4j is a Java-based logging utility. It was originally written by Ceki Gülcü and is part of the Apache Logging Services project of the Apache Software Foundation. Log4j is one of several Java logging frameworks. Gülcü has since started the SLF4J and Logback projects, with the intention of offering a successor to Log4j.
Apache Log4j 2 is the successor of Log4j 1 which was released as GA version in July 2014. The framework was rewritten from scratch and has been inspired by existing logging solutions, including Log4j 1 and java.util.logging.
Log4j log levels
Level | Description |
OFF | The highest possible rank and is intended to turn off logging. |
FATAL | Severe errors that cause premature termination. Expect these to be immediately visible on a status console. |
ERROR | Other runtime errors or unexpected conditions. Expect these to be immediately visible on a status console. |
WARN | Use of deprecated APIs, poor use of API, ‘almost’ errors, other runtime situations that are undesirable or unexpected, but not necessarily “wrong”. Expect these to be immediately visible on a status console. |
INFO | Interesting runtime events (startup/shutdown). Expect these to be immediately visible on a console, so be conservative and keep to a minimum. |
DEBUG | Detailed information on the flow through the system. Expect these to be written to logs only. Generally speaking, most lines logged by your application should be written as DEBUG. |
TRACE | Most detailed information. Expect these to be written to logs only. Since version 1.2.12. |
Add log4j to Talend Studio
- Go to Talend Studio -> Edit Project Properties.
2. Select the Log4j.
3. Add below Appender code under Configuration.
<appender name="LOGGER" class="org.apache.log4j.FileAppender">
<param name="file" value="<FILE LOCATION>/talend_job.log" />
<param name="append" value="false" />
<param name="threshold" value="debug" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{yyyy MMM dd hh:mm:ss,SSS a zzz}] [%-5p]: %c - %m%n" />
</layout>
</appender>
Mnemonic | Description |
%p | Used to output the priority for INFO, DEBUG, TRACE, etc. |
%c | Used to output the category for fully qualified project name and job name. |
%m | Used to output the application supplied message. |
%n | Used to output the platform-specific newline character or characters. |
%d | Used to output the get current date. |
4. Add below code Appender-ref code under Configuration -> Root.
<appender-ref ref="LOGGER"/>
5. In case, if you wanted to add custom logs using Java Component.
import org.apache.log4j.Logger;
final Logger logger = Logger.getLogger(this.getClass().getName());
logger.fatal(message);
logger.error(message);
logger.warn(message);
logger.info(message);
logger.debug(message);
logger.trace(message);
6. Happy Learning!!
Hi Girish
Can I achieve the same thing in the Log4j2 in v7.3.1 ?
– Apoorv Tyagi
LikeLike