【发布时间】:2014-08-15 21:22:01
【问题描述】:
这是我的 log4j.properties
# Define the root logger with appender file
log4j.rootLogger = DEBUG,FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=/media/.I have given the whole path../MyProject/log.log
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d{MM-dd@HH:mm:ss} %-5p (%13F:%L) %3x - %m%n
这是我的 java 文件
package examples;
import java.util.logging.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Test {
static Logger logger = Logger.getLogger(Test.class.getName());
public static void main(String a[]){
//PropertiesConfigurator is used to configure logger from properties file
PropertyConfigurator.configure("log4j.properties");
//Log in console in and log file
//logger.debug("Log4j appender configuration is successful !!");
logger.info("Log4j appender configuration is successful !!");
}
}
我仍然在控制台上得到输出消息,只有我的 log.log 文件是空的。 我对文件有写权限 我在eclipse中运行这个是个问题。
【问题讨论】:
-
可能配置文件没有被正确选择。添加-Dlog4j.debug,你会看到log4j调试信息包含configruation。
-
@xwid 如果我在其中更改某些内容,则会给我错误,所以我认为它正在被拾取
-
我使用了你的配置,只是将 appender 的文件更改为 test-log.log,log4j 创建了这个文件,其中包含日志消息。