1.我看的是孙鑫的书上的例子,那上面说要把配置文件(hello.lcf)放到和.class文件同一个目录下;
2.因为书上只有在控制台java Hello hello.lcf这样执行程序;没有说在eclipse里配置文件放哪儿?————我试了一下也把hello.lcf放到bin目录,或者src目录,或者2个目录都放了那个配置文件,但都是找不到配置文件!
错误信息:
log4j:ERROR Could not read configuration file [hello.lcf].
java.io.FileNotFoundException: hello.lcf (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:297)
at org.apache.log4j.PropertyConfigurator.configure(PropertyConfigurator.java:315)
at Hello.main(Hello.java:13)
log4j:ERROR Ignoring configuration file [hello.lcf].
log4j:WARN No appenders could be found for logger (Hello).
log4j:WARN Please initialize the log4j system properly.
------------------
1 Hello.java 2 import org.apache.log4j.Logger; 3 //import org.apache.log4j.BasicConfigurator; 4 import org.apache.log4j.PropertyConfigurator; 5 6 public class Hello 7 { 8 9 static Logger logger = Logger.getLogger(Hello.class); 10 11 public static void main(String args[]) 12 { 13 //BasicConfigurator.configure(); 14 PropertyConfigurator.configure("hello.lcf"); 15 16 logger.debug("Hello world."); 17 logger.info("Welcome you."); 18 } 19 }
Hello.lcf
#设置根记录器的日志级别为DEBUG,Appender为A1
log4j.rootLogger=DEBUG,A1
#设置A1为ConsoleAppender
log4j.appender.A1=org.apache.log4j.ConsoleAppender
#A1使用PatternLayout
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%r [%t] %p %c %x - %m%n
你那个Hello类是做什么的?
路径应该为:
src/配置文件