I’m facing a problem with log4j and spring JDBC. I may have missed something so don’t hesitate to correct me in case of misunderstanding.
I’m currently using a log4j.xml file
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %p %C{1}.%M - %m%n" />
</layout>
</appender>
<appender name="stationPdf" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="./log/stationPdf.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="5000KB" />
<param name="MaxBackupIndex" value="3" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %p %C{1}.%M - %m%n" />
</layout>
</appender>
<category name="com.mypackage">
<level value="debug" />
<appender-ref ref="stationPdf" />
</category>
<category name="org.springframework.jdbc">
<level value="debug" />
<appender-ref ref="stationPdf" />
</category>
<root>
<level value="debug" />
<appender-ref ref="console" />
<appender-ref ref="stationPdf" />
</root>
As you could see, it’s mainly redirecting everyhting in a specific file.
The main problem is linked with spring jdbc. I’m using a small database to store properties and I need to connect to this database to extract data.
So, when i make a mistake in the password, i get the following exception, as expected:
Exception in thread "AWT-EventQueue-0" org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user 'root'@'localhost' (using password: YES))
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:573)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:637)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:666)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:674)
at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:729)
at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:749)
at com.mypackage.dao.impl.PropertiesDaoJdbcImpl.selectProperty(PropertiesDaoJdbcImpl.java:37)
at com.mypackage.data.ExtractedDataClass.loadProperties(ExtractedDataClass.java:42)
at com.mypackage.accueil.Accueil.<init>(Accueil.java:138)
at com.mypackage.accueil.Accueil$12.run(Accueil.java:896)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
[...]
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Access denied for user 'root'@'localhost' (using password: YES))
at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1549)
[...]
Caused by: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4098)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4030)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:951)
[...]
The problem is that I can’t get this exception in my log file. It appears in the console, but never in my file.
What should I use, add or delete to my log4j.xml file to get these exceptions?
Thanks in advance,
Sorry for my approximative English 🙂
The fact that the error message starts
suggests that it’s not being logged through log4j at all, but simply appearing on the console because of the standard “uncaught exception” logic that every Java thread gets by default, and this would explain why your log4j settings are not having any effect. You’ll need to catch the exception at a suitable point in your own code and log it yourself.