I want to log out SQL statements that I executed in log4j.

Asked 1 years ago, Updated 1 years ago, 269 views

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");
    }
}

java spring logging

2022-10-27 00:00

1 Answers

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.