我做了一个简单的类,并尝试使用Logger类方法打印日志消息,并使用FileAppender将日志消息附加到文件中。 但是,日志不会打印在文件中。使用log4j时没有得到预期的输出
任何人都可以指导我如何使用我制作的程序在文件中打印这些日志。 我用在类路径的log4j-1.2.17 API:
代码为以下程序:
public class Client {
static Logger l=Logger.getLogger(Client.class.getName());
public static void main(String[] args) {
Layout l1=new SimpleLayout();
Appender a;
try{
a=new FileAppender(l1,"my.txt",true);
l.debug("Hello Jc");
l.info("Hello Jc");
l.fatal("This is not the error message");
l.addAppender(a);
}
catch(Exception e){
}
System.out.println("your logic executed Successfully");
// TODO Auto-generated method stub
}
输出:
log4j:WARN No appenders could be found for logger (Client).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
your logic executed Successfully
在文件
预期输出:
DEBUG Hello Jc
INFO Hello Jc
FATAL This is not the error message
你有log4j.properties或log4j.xml文件在你的类路径附加器为您的类和包? – SMA
不,我没有使用.properties或xml,但我用一个简单的java类来拥有appender和布局对象。 – user6389648