Talend Talend Integration

Write console logs to log files in Talend Stutio

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

LevelDescription
OFFThe highest possible rank and is intended to turn off logging.
FATALSevere errors that cause premature termination. Expect these to be immediately visible on a status console.
ERROROther runtime errors or unexpected conditions. Expect these to be immediately visible on a status console.
WARNUse 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.
INFOInteresting runtime events (startup/shutdown). Expect these to be immediately visible on a console, so be conservative and keep to a minimum.
DEBUGDetailed 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.
TRACEMost detailed information. Expect these to be written to logs only. Since version 1.2.12.

Add log4j to Talend Studio

  1. 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>
MnemonicDescription
%pUsed to output the priority for INFO, DEBUG, TRACE, etc.
%cUsed to output the category for fully qualified project name and job name.
%mUsed to output the application supplied message.
%nUsed to output the platform-specific newline character or characters.
%dUsed 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!!

1 comment

Leave a comment

Talend Best Practices

Feel free to reach out to us with any questions at : solutions@thinkartha.com