1. 程式人生 > >使用log4j控制檯報錯

使用log4j控制檯報錯

log4j:ERROR Could not instantiate class [log4j].
java.lang.ClassNotFoundException: log4j
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:191)
    at org.apache.log4j.helpers.Loader.loadClass(Loader.java:198)
    at org.apache.log4j.helpers.OptionConverter.instantiateByClassName(OptionConverter.java:327)
    at org.apache.log4j.helpers.OptionConverter.instantiateByKey(OptionConverter.java:124)
    at org.apache.log4j.PropertyConfigurator.parseAppender(PropertyConfigurator.java:785)
    at org.apache.log4j.PropertyConfigurator.parseCategory(PropertyConfigurator.java:768)
    at org.apache.log4j.PropertyConfigurator.configureRootCategory(PropertyConfigurator.java:648)
    at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:514)
    at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:580)
    at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:526)
    at org.apache.log4j.LogManager.<clinit>(LogManager.java:127)
    at org.slf4j.impl.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:66)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:270)
    at org.apache.ibatis.logging.slf4j.Slf4jImpl.<init>(Slf4jImpl.java:33)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.apache.ibatis.logging.LogFactory.setImplementation(LogFactory.java:127)
    at org.apache.ibatis.logging.LogFactory.useSlf4jLogging(LogFactory.java:87)
    at org.apache.ibatis.logging.LogFactory$1.run(LogFactory.java:36)
    at org.apache.ibatis.logging.LogFactory.tryImplementation(LogFactory.java:117)
    at org.apache.ibatis.logging.LogFactory.<clinit>(LogFactory.java:34)
    at org.apache.ibatis.datasource.pooled.PooledDataSource.<clinit>(PooledDataSource.java:42)
    at org.apache.ibatis.datasource.pooled.PooledDataSourceFactory.<init>(PooledDataSourceFactory.java:26)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at java.lang.Class.newInstance(Class.java:379)
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.dataSourceElement(XMLConfigBuilder.java:274)
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.environmentsElement(XMLConfigBuilder.java:232)
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:104)
    at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:92)
    at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
    at org.apache.ibatis.session.SqlSessionManager.newInstance(SqlSessionManager.java:54)
    at com.tradition.demo.MybatisTest.main(MybatisTest.java:25)
log4j:ERROR Could not instantiate appender named "keyfile".

[20:43:56.953] [DEBUG] org.apache.ibatis.logging.LogFactory.setImplementation(LogFactory.java:128) Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.

報錯上看是缺少log4j的類,我也引用了log4j的包了,為啥還會報錯呢?而且類名也不完整,結合下面控制檯日誌列印,說明log4j.properties配置生效了,應該是在檔案中列印日誌配置有問題。經過檢查發現第8行配置有誤,應該是log4j.appender.keyfile=org.apache.log4j.FileAppender。

隨便從網上找的配置,果然還要自己理解一下,不然出錯都不知道哪裡的問題,第10行配置也有誤,汗