My name is Veera. I'm a full stack web developer. I have founded http://www.timethetask.com and currently working on my next product. Veera is a DZone MVB and is not an employee of DZone and has posted 33 posts at DZone. You can read more from them at their website. View Full User Profile

Log4j Tutorial – Writing different log levels in different log files

05.18.2011
| 16223 views |
  • submit to reddit

Recently one of my blog reader Surisetty send me a question, asking me if it is possible to write log messages of different levels (info, debug, etc) into different log files? To answer his question, yes, it is possible. We can do this by extending the FileAppender class and writing our own logic.

Below is the proof of concept code written to demonstrate this. Before that, you can download the Eclipse project file to run this code in your environment.

Download the Source code

To write different log levels in different log files

  1. Create a custom Log4j appender extending FileAppender.
  2. In that, override the append() method and check for the log level before writing a log message. Based on the level, call the setFile() method to switch between corresponding log file.
  3. Also, use MDC to store the original log file name mentioned in the log4j.properties. This is needed because setFile() changes the log file name every time you call it. So, we need to keep a track of the original file name somehow. And, we can use Log4j MDC for this.

Custom Appender: LogLevelFilterFileAppender

 

package com.veerasundar.log4j;


import java.io.File;
import java.io.IOException;

import org.apache.log4j.FileAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.MDC;
import org.apache.log4j.spi.ErrorCode;
import org.apache.log4j.spi.LoggingEvent;

/**
* This customized Log4j appender will seperate the log messages based on their
* LEVELS and will write them' into separate files. For example, all DEBUG
* messages will go to a file and all INFO messages will go to a different file.
*
* @author Veera Sundar | http://veerasundar.com
*
*/
public class LogLevelFilterFileAppender extends FileAppender {

private final static String DOT = ".";
private final static String HIPHEN = "-";
private static final String ORIG_LOG_FILE_NAME = "OrginalLogFileName";

public LogLevelFilterFileAppender() {

}

public LogLevelFilterFileAppender(Layout layout, String fileName,
boolean append, boolean bufferedIO, int bufferSize)
throws IOException {
super(layout, fileName, append, bufferedIO, bufferSize);
}

public LogLevelFilterFileAppender(Layout layout, String fileName,
boolean append) throws IOException {
super(layout, fileName, append);
}

public LogLevelFilterFileAppender(Layout layout, String fileName)
throws IOException {
super(layout, fileName);
}

@Override
public void activateOptions() {
MDC.put(ORIG_LOG_FILE_NAME, fileName);
super.activateOptions();
}

@Override
public void append(LoggingEvent event) {
try {
setFile(appendLevelToFileName((String) MDC.get(ORIG_LOG_FILE_NAME),
event.getLevel().toString()), fileAppend, bufferedIO,
bufferSize);
} catch (IOException ie) {
errorHandler
.error(
"Error occured while setting file for the log level "
+ event.getLevel(), ie,
ErrorCode.FILE_OPEN_FAILURE);
}
super.append(event);
}

private String appendLevelToFileName(String oldLogFileName, String level) {
if (oldLogFileName != null) {
final File logFile = new File(oldLogFileName);
String newFileName = "";
final String fn = logFile.getName();
final int dotIndex = fn.indexOf(DOT);
if (dotIndex != -1) {
// the file name has an extension. so, insert the level
// between the file name and the extension
newFileName = fn.substring(0, dotIndex) + HIPHEN + level + DOT
+ fn.substring(dotIndex + 1);
} else {
// the file name has no extension. So, just append the level
// at the end.
newFileName = fn + HIPHEN + level;
}
return logFile.getParent() + File.separator + newFileName;
}
return null;
}
}

log4j.properties file

log4j.rootLogger = DEBUG, fileout
log4j.appender.fileout = com.veerasundar.log4j.LogLevelFilterFileAppender
log4j.appender.fileout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c - %m%n
log4j.appender.fileout.layout = org.apache.log4j.PatternLayout
log4j.appender.fileout.File = C:/vraa/temp/logs.log

Lets test our code

package com.veerasundar.log4j;

import org.apache.log4j.Logger;

public class Log4jDemo {

private static final Logger logger = Logger.getLogger(Log4jDemo.class);

public static void main(String args[]) {
logger.debug("This is a debug message");
logger.info("This is a information message");
logger.warn("This is a warning message");
logger.error("This is an error message");
logger.fatal("This is a fatal message");
logger.debug("This is another debug message");
logger.info("This is another information message");
logger.warn("This is another warning message");
logger.error("This is another error message");
logger.fatal("This is another fatal message");
}
}

 Download the Source code

From http://veerasundar.com/blog/2011/05/log4j-tutorial-writing-different-log-levels-in-different-log-files/

Published at DZone with permission of Veera Sundar, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Abhishek Chavan replied on Thu, 2011/05/19 - 2:06am

Doesn't Log4j already provide such functionality to write fifferent Status messages to different files?

Paul Etherington replied on Thu, 2011/05/19 - 2:21am

The first thing to say is that I'd question the use case a bit further. I can't see why you'd want INFO level messages in a file without ERROR (etc) level to give you the full context. It smells like either you're using the log levels for a purpose other than you intended, or your logging strategy is unusual. I have seen in the past a requirement to have different log levels AND ABOVE in different files. i.e. ERROR and above to one file, INFO and above to another. This means the ERROR file can be monitored and an alarm triggered on a change of size, whereas the INFO file can then be used to diagnose the problem. In log4j, I believe this is achievable through using multiple appenders and the Threshold parameter. If the original requirement is correct (i.e. my smells are unjustified), then I think you can still achieve the same result by using multiple appenders. The code would be a lot simpler (no need to store/retrieve filenames), at the cost of more configuration.

Mladen Girazovski replied on Thu, 2011/05/19 - 5:30am in response to: Abhishek Chavan

Yes, log4j provides this funcionality out of the box, solely by configuration, no need to write your own appenders at all.

log4j has a successor, logback, which includes several improvements, check it out: http://logback.qos.ch/

Jagannathan Asokan replied on Thu, 2011/05/19 - 8:47am

One can use Apache Chainsaw or Mindtree Insight for analysing the log files!

Emma Watson replied on Fri, 2012/03/30 - 12:26pm

There has been no real development on Log4j in the last 4-5 years. Slf4J (created by the same guy as Log4J) is effectively Log4J 2.

Swing

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.