logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Ravi Roy" <ravi.a...@gmail.com>
Subject Project containing multiple packages : Log4j does not write to log file.
Date Thu, 11 Dec 2008 07:12:26 GMT
Hi All,

I am new to Log4j, so forgive me in case I am asking too trivial quesion.

The follwing code works fine, when I create a new project in Eclipse
and run that code, it creates log file under c:\Temp, but when same
file I copy to a existing project having multiple packages, it does
not work and log file gets created under c:\Temp but nothing is
written in that.

Can some body tell me where I am wrong please ?

Regards
Ravi.



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

import org.apache.log4j.Logger;
import org.apache.log4j.Category;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;
import org.apache.log4j.Category;
import org.apache.log4j.Level;


/**
 * Logging Sample.
 * @author Ravi
 *
 */
public class TestLogging
{
    public static final Logger aLogger = Logger.getLogger(TestLogging.class);
    String aLogFile;


    public void doLogging ()
    {

        aLogger.info("test log");
    }


    public static void main (String[] args)
    {
        TestLogging testLogging = new TestLogging();
        testLogging.startLogging();
        testLogging.doLogging();
    }


    public void startLogging ()
    {
        PatternLayout layout = new PatternLayout("%d [%p] %c %m%n");
        String logFolder = "C:\\Temp";
        aLogFile = logFolder + "\\" + "TestLogging" + ".log";
        ConsoleAppender con = new ConsoleAppender(layout);
        con.setName("ConsoleLog");
        con.setThreshold(Level.INFO);
        aLogger.addAppender(con);

        try
        {
            File aFile = new File(logFolder);
            aFile.mkdirs();
            RollingFileAppender file = new RollingFileAppender(layout,
aLogFile);
            file.setName("FileLog");
            file.setMaxFileSize("1MB");
            file.setThreshold(Level.INFO);
            aLogger.addAppender(file);
        }
        catch (IOException ex)
        {
            aLogger.warn("Failed to create file appender", ex);
        }
    }


}

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Mime
View raw message