Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
884 views
in Technique[技术] by (71.8m points)

java - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...