How to initialize log4j properly?

2018-12-31 09:57发布

After adding log4j to my application I get the following output every time I execute my application:

log4j:WARN No appenders could be found for logger (slideselector.facedata.FaceDataParser).
log4j:WARN Please initialize the log4j system properly.

It seems this means a configuration file is missing. Where should this config file be located and what is a good start content?

I'm using plain java for developing a desktop application. So no webserver etc...

20条回答
爱死公子算了
2楼-- · 2018-12-31 10:46

You can set the location of your log4j.properties from inside your java app by using:

org.apache.log4j.PropertyConfigurator.configure(file/location/log4j.properties)

More information is available here: https://logging.apache.org/log4j/1.2/manual.html

查看更多
看风景的人
3楼-- · 2018-12-31 10:47

For testing, a quick-dirty way including setting log level:

org.apache.log4j.BasicConfigurator.configure();
org.apache.log4j.Logger.getRootLogger().setLevel(org.apache.log4j.Level.WARN);

// set to Level.DEBUG for full, or Level.OFF..
查看更多
登录 后发表回答