Loggers in Java, Redirecting Log contents in a Log file in Java
Dear reader,
Here is the complete coding where we can configure a logger and redirect the logger contents
into a separate file for proper tracing.
//LoggerTest.java
package com.ewp.services;
import java.util.logging.*;
import java.io.*;
public class LoggerTest {
public static Logger logger;
static {
try {
boolean append = true;
FileHandler fh = new FileHandler("D://Temp//log.properties", append);
fh.setFormatter(new Formatter() {
public String format(LogRecord rec) {
StringBuffer buf = new StringBuffer(1000);
buf.append(new java.util.Date());
buf.append("===>");
buf.append(formatMessage(rec));
buf.append('\n');
return buf.toString();
}
});
logger = Logger.getLogger("TestLog");
logger.addHandler(fh);
}
catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
logger.severe("Severe category messages");
logger.warning("Warning categary messages");
logger.info("Info level messages");
}
}
//Output in file "D:\Temp\log.properties"
/*
Mon Aug 29 15:39:48 IST 2011===>Severe category messages
Mon Aug 29 15:39:49 IST 2011===>Warning categary messages
Mon Aug 29 15:39:49 IST 2011===>Info level messages
*/
Monday, August 29, 2011
Loggers in Java, Redirecting Log contents in a Log file in Java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment