Talend Tip: error logging with Apache log4j

In an earlier blog, we explained how to use tLogCatcher to catch errors as they occur in the job and print them to the console. This time, we are going to improve the error logging by utilizing the famous error logging framework from Apache, log4j.

 

Firstly, you have to create a routine in Talend (we named it ApacheLog4jLogger in this example) and instantiate the Apache logger:
/* =============== Sample routine in Talend ===================*/
public class ApacheLog4jLogger {
  public static final Logger logger = Logger.getRootLogger();

  static {
    try {
      final File logDir = new File("logs");
      logDir.mkdirs();

      final File logFile = new File(logDir + "/mylog4j.log");
      
      final PatternLayout pl = new PatternLayout("%d %-5p: %m%n");
      final DailyRollingFileAppender dailyfp = new DailyRollingFileAppender( pl, logFile.getCanonicalPath(), ".yyyy-MM-dd" );
      
      logger.setLevel( Level.INFO );
      logger.addAppender( dailyfp );
      
      final ConsoleAppender cp = new ConsoleAppender( pl, "System.out" );
      cp.setImmediateFlush( true );

      logger.addAppender( cp );
      
    } catch ( Exception e ) {
      e.printStackTrace( System.err );
    }
  }
  
  ....
  ....
}
Once you have done this, you will need to create a static function in the routine that will accept the error message from the Talend job:
public static void writeLogFile(String message, String level, String errorCode) {
  if(level.equalsIgnoreCase("info")){
    logger.info(errorCode + " - " + message);
  } else if(level.equalsIgnoreCase("warn")){
    logger.warn(errorCode + " - " + message);
  } else if(level.equalsIgnoreCase("error")){
    logger.error(errorCode + " - " + message);
  }
}
The function will accept error messages and error codes and write the error to the log file according to the logging level (info, warn or error). Now we have completed creating the routine. Next, we will show you how to log the error by using this routine.
In order for Talend to build the job correctly, we have to load the log4j library into classpath by using the tLibraryLoad component as show below:
Now, you just need to replace the tLogRow with tJavaRow and link it to the tLogCatcher. Ah yes, you have to call the ApacheLog4jLogger routine within the tJavaRow in order to log the error to the log file.
Yay!! You have successfully created a job that uses log4j. You should see a log file created in the logs folder when the job encounters an exception.

Get in touch today to see how WDCi can help your business.