Log file is empty (log4j)

一个人想着一个人 提交于 2019-12-23 01:37:14

问题


Log information is shown on console but not on the log file (the file is created but no content is added). What's wrong?

Source:

package mytest;

import java.util.logging.Level; import java.util.logging.Logger;

import org.apache.log4j.PropertyConfigurator;

public class Main {     
    public static void main(String[] args) {        
        Logger log  = Logger.getLogger(Main.class.getName());
        log.setLevel(Level.ALL);        
        log.info("A line)");
        PropertyConfigurator.configure("log.properties");       
        log.info("Another line");   
    } 
}

log.properties file:

log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log

log4j.appender.R.MaxFileSize=100KB

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

回答1:


You are mix things, java.util.logging and log4j.

You can try to use commonsLoggin with log4j

http://commons.apache.org/logging/guide.html#Quick%20Start

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
...
Log log = LogFactory.getLog(getClass());


来源:https://stackoverflow.com/questions/13288393/log-file-is-empty-log4j

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!