Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4J creates log file but does not write to it

I'm trying to configure log4j to log messages to a file. Right now, the file does get created with the name I provide, but the logs are not written to the file. My code:

public class Main extends Application
{
    private static Logger logger = Logger.getLogger( Main.class.getName() );
    @Override
    public void start(Stage primaryStage) throws Exception
    {
        logger.warning("test");
        logger.severe("oh noes");
    }


    public static void main(String[] args) {
        launch(args);
    }
}

Contents of my log4j.properties file:

# Root logger option
log4j.rootLogger=INFO, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=log.log
log4j.appender.file.MaxFileSize=1024MB
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

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

When I run this, I get this output in the console:

Jun 23, 2014 3:19:16 AM com.foo.Main start
WARNING: test
Jun 23, 2014 3:19:16 AM com.foo.Main start
SEVERE: oh noes

The file log.log does get created in my main directory. But its empty.

Any ideas what I'm doing wrong? I'm using log4j version 1.2.17.

like image 406
Ali Avatar asked Jun 22 '14 22:06

Ali


1 Answers

The output seems to be of the default format that Java's standard logging framework (JUL) would emit.

So, there are two possibilities (that come to mind):

  1. Your code imports java.util.logging.Logger, rather than org.apache.log4j.Logger.
  2. There exists a library of some sort, in your classpath, that intercepts Log4J calls and converts them to JUL calls.
like image 198
Isaac Avatar answered Oct 26 '22 17:10

Isaac