我正在尝试将log4j数据库日志配置到一个maven web项目中,但无法使其正常工作。如果有人能指出我做错了什么就好了。
我创建了一个maven web项目,并将其部署在JBoss通配符中。
我添加了这两个依赖项。
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
我在资源项下添加了log4j.properties文件。
log4j.rootLogger = DEBUG, DB
log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender
log4j.appender.DB.URL=jdbc:mysql://localhost:3306/pos_test
log4j.appender.DB.driver=com.mysql.jdbc.Driver
log4j.appender.DB.user=root
log4j.appender.DB.password=password
log4j.appender.DB.sql=INSERT INTO myLogs VALUES('%X{User}','%d{yyyy-MM-dd HH:mm:ss}','%C','%p','%m')
log4j.appender.DB.layout=org.apache.log4j.PatternLayout
在我的代码中,我尝试按以下方式记录,
logger.info("Testing info");
logger.debug("Testing Debug");
我使用以下sql创建了数据库表。
CREATE TABLE myLogs (
USER_ID varchar(20) NOT NULL,
LOG_DATE date NOT NULL,
LOGGER varchar(50) NOT NULL,
LEVEL varchar(10) NOT NULL,
MESSAGE varchar(1000) NOT NULL
);
日志中没有错误,而且记录器信息也没有打印在服务器日志上。
但是,日志信息没有添加到数据库中。
我在这里错过了什么?
致以敬意,
发布于 2015-09-07 04:48:28
对我来说,,概念上,你做的一切都是正确的。
我对野蝇知之甚少,但我试着做以下几点:
- Open your WAR/EAR with your favorite tool (winrar or something) and see whether the logging properties file really exists in the correct place (WEB-INF\classes in the case of WAR).
- The same goes for log4j dependency - it should be packed correctly into the 'lib' folder
希望这能有所帮助
标记
https://stackoverflow.com/questions/32430865
复制相似问题