Outputs application logs before and after a specific method using Aspect.
You are about to log SQL statements that were executed in JdbcTemplate.
All logs except SQL statements are printed correctly.
I don't use log4jdbc.
To retrieve logs, modify the LogApp class side below or
I think that there are two places in the TestDao class where Logger writes individual logs.
Failed to resolve how execution SQL statements can be retrieved.
Please give me some advice.
Desired Log
SELECT* FROM tableA WHERE name="testName";
Or placeholder SQL statement and parameter set
log4j.xml
<logger name="org.springframework.jdbc.core.JdbcTemplate"additivity="false">
<level value="debug"/>
<appender-refref="stdout"/>
</logger>
<logger name="org.springframework.jdbc.core.StatementCreatorUtils"additivity="false">
<level value="debug"/>
<appender-refref="stdout"/>
</logger>
<root>
<appender-refref="stdout"/>
</root>
Log Output Definitions
@Component
@ Aspect
publicclassLogApp{
private static final logger logger = Logger.getLogger(LogApp.class);
@Before("execution(*jp.co.test.*.*(...))")")
public void aa (JoinPoint jp) {
log.debug(jp.getSignature().getDeclaringTypeName());
}
@After("execution(*jp.co.test.*.*(...))")")
public void bb(JoinPoint jp) {
log.debug(jp.getSignature().getDeclaringTypeName());
}
}
DAO Class
package jp.co.test;
public class TestDao{
@Autowired
private JdbcTemplate jdbc;
public void selectName(){
jdbc.queryForList("SELECT* FROM tableA WHERE name=?", "testName");
}
}
The SQL body is printed at the debug
level, but the prepared statement value is trace
level, so the configuration looks like this:
<logger name="org.springframework.jdbc.core.JdbcTemplate"additivity="false">
<level value="debug"/>
<appender-refref="stdout"/>
</logger>
<logger name="org.springframework.jdbc.core.StatementCreatorUtils"additivity="false">
<level value="trace"/>
<appender-refref="stdout"/>
</logger>
Results:
Executing prepared SQL query
Executing prepared SQL statement [SELECT* FROM tableA WHERE name=?]
Setting SQL statement parameter value: column index 1, parameter value [testName], value class [java.lang.String], SQL type unknown
© 2024 OneMinuteCode. All rights reserved.