Here i'll be explaining you the Log4j configuration in Sturts1.x with a sample Java Program.
import org.apache.log4j.*;
public class Log4jClass {
//In order to generate the logger file at the time of starting the server.
static{
//create a logger instance
Logger log = Logger.getLogger(Log4jClass.class);
//Domconfigurator to setting the properties of logger file using xml or properties file.
DomConfigurator.configure("log4jpath/log4j.xml"); //or log4j.properties file
}
public static void main(String args[]) {
//Different logging levels available in Log4j
Log4jClass.trace(Log4jClass.class,"This is the trace method usage in log4j file");
Log4jClass.debug(Log4jClass.class,"This is the debug method usage in log4j file");
Log4jClass.info(Log4jClass.class,"This is the info method usage in log4j file");
Log4jClass.warn(Log4jClass.class,"This is the warn method usage in log4j file");
Log4jClass.error(Log4jClass.class,"This is the error method usage in log4j file");
Log4jClass.fatal(Log4jClass.class,"This is the fatal method usage in log4j file");
}}
You can set the System Out console to logger also using the System.setOut() method
Where,
PrintStream ps = new PrintStream(new FileOutputStream("pathtofile");
System.setOut(ps);
Now,
System.out.println("Text");
will be moved to the logger file only but not to system console. Any line before the System.setOut() will be to System Console but not to logger file.
Any modifications or corrections will be appreciated......