What is log4j's default log file dumping path

manisha picture manisha · Jun 27, 2013 · Viewed 106k times · Source

Hi i am new to programming concepts and i am tend to work out something with log4j. So i am reading Log4j tutorials where i found the following code:

package test;
import org.apache.log4j.Logger;
import java.io.*;
import java.sql.SQLException;


public class Log4jExample {

    /* Get actual class name to be printed on */
        static Logger log = Logger.getLogger(Log4jExample.class.getName());
        public static void main(String[] args)throws IOException,SQLException
        {
            log.debug("Hello this is an debug message");
            log.info("Hello this is an info message");
        }

}

But after running this in eclipse i am not able to locate the generated log file. Can anybody tell where is the file being dumped? Also help me with some best sites wherefrom i can learn Log4j and Java Doc from the scratch. Thanks!!

Answer

Juned Ahsan picture Juned Ahsan · Jun 27, 2013

To redirect your logs output to a file, you need to use the FileAppender and need to define other file details in your log4j.properties/xml file. Here is a sample properties file for the same:

# Root logger option
log4j.rootLogger=INFO, file

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\loging.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

Follow this tutorial to learn more about log4j usage:

http://www.mkyong.com/logging/log4j-log4j-properties-examples/