42 lines
1.2 KiB
Java
42 lines
1.2 KiB
Java
package proxy;
|
|
|
|
import java.time.Instant;
|
|
|
|
/**
|
|
* Logging Proxy — adds timing and audit logging around every query
|
|
* without touching RealDatabaseConnection or any of its callers.
|
|
*
|
|
* This is the "cross-cutting concern" use case of Proxy,
|
|
* the same mechanism behind Spring AOP's @Around advice.
|
|
*/
|
|
public class LoggingProxy implements DatabaseConnection {
|
|
|
|
private final DatabaseConnection target;
|
|
|
|
public LoggingProxy(DatabaseConnection target) {
|
|
this.target = target;
|
|
}
|
|
|
|
@Override
|
|
public void connect() {
|
|
System.out.println("[Log] connect() at " + Instant.now());
|
|
target.connect();
|
|
}
|
|
|
|
@Override
|
|
public String executeQuery(String sql) {
|
|
long start = System.currentTimeMillis();
|
|
System.out.println("[Log] QUERY START: " + sql);
|
|
String result = target.executeQuery(sql);
|
|
long elapsed = System.currentTimeMillis() - start;
|
|
System.out.println("[Log] QUERY END: " + elapsed + "ms | result: " + result);
|
|
return result;
|
|
}
|
|
|
|
@Override
|
|
public void disconnect() {
|
|
System.out.println("[Log] disconnect() at " + Instant.now());
|
|
target.disconnect();
|
|
}
|
|
}
|