-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add the doma-slf4j module * Add child loggers
- Loading branch information
1 parent
9eb6bbd
commit 101464e
Showing
7 changed files
with
478 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/.classpath | ||
/.gradle/ | ||
/.project | ||
/.settings/ | ||
/bin/ | ||
/build/ | ||
/target/ | ||
/.metadata | ||
.idea | ||
out | ||
.factorypath |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
description = "doma-slf4j" | ||
|
||
dependencies { | ||
api("org.slf4j:slf4j-api:1.7.30") | ||
implementation(project(":doma-core")) | ||
testImplementation("ch.qos.logback:logback-classic:1.2.3") | ||
} |
19 changes: 19 additions & 0 deletions
19
doma-slf4j/src/main/java/org/seasar/doma/jdbc/LogKind.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.seasar.doma.jdbc; | ||
|
||
public enum LogKind { | ||
DAO, | ||
SKIP, | ||
SQL, | ||
LOCAL_TRANSACTION, | ||
FAILURE; | ||
|
||
private final String fullName; | ||
|
||
LogKind() { | ||
this.fullName = getClass().getName() + "." + name(); | ||
} | ||
|
||
public String fullName() { | ||
return fullName; | ||
} | ||
} |
309 changes: 309 additions & 0 deletions
309
doma-slf4j/src/main/java/org/seasar/doma/jdbc/Slf4jJdbcLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,309 @@ | ||
package org.seasar.doma.jdbc; | ||
|
||
import java.sql.SQLException; | ||
import java.util.Objects; | ||
import java.util.function.Supplier; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.slf4j.Marker; | ||
import org.slf4j.MarkerFactory; | ||
import org.slf4j.event.Level; | ||
|
||
public class Slf4jJdbcLogger extends AbstractJdbcLogger<Level> { | ||
|
||
public Slf4jJdbcLogger() { | ||
super(Level.DEBUG); | ||
} | ||
|
||
public Slf4jJdbcLogger(Level level) { | ||
super(level); | ||
} | ||
|
||
@SuppressWarnings("CStyleArrayDeclaration") | ||
@Override | ||
protected void logDaoMethodEntering( | ||
String callerClassName, | ||
String callerMethodName, | ||
Object args[], | ||
Level level, | ||
Supplier<String> messageSupplier) { | ||
log(LogKind.DAO.fullName(), level, callerClassName, callerMethodName, null, messageSupplier); | ||
} | ||
|
||
@Override | ||
protected void logDaoMethodExiting( | ||
String callerClassName, | ||
String callerMethodName, | ||
Object result, | ||
Level level, | ||
Supplier<String> messageSupplier) { | ||
log(LogKind.DAO.fullName(), level, callerClassName, callerMethodName, null, messageSupplier); | ||
} | ||
|
||
@Override | ||
protected void logDaoMethodThrowing( | ||
String callerClassName, | ||
String callerMethodName, | ||
RuntimeException e, | ||
Level level, | ||
Supplier<String> messageSupplier) { | ||
log(LogKind.DAO.fullName(), level, callerClassName, callerMethodName, null, messageSupplier); | ||
} | ||
|
||
@Override | ||
protected void logSqlExecutionSkipping( | ||
String callerClassName, | ||
String callerMethodName, | ||
SqlExecutionSkipCause cause, | ||
Level level, | ||
Supplier<String> messageSupplier) { | ||
String loggerName = LogKind.SKIP.fullName() + "." + cause.name(); | ||
log(loggerName, level, callerClassName, callerMethodName, null, messageSupplier); | ||
} | ||
|
||
@Override | ||
protected void logSql( | ||
String callerClassName, | ||
String callerMethodName, | ||
Sql<?> sql, | ||
Level level, | ||
Supplier<String> messageSupplier) { | ||
String loggerName = LogKind.SQL.fullName() + "." + sql.getKind().name(); | ||
log(loggerName, level, callerClassName, callerMethodName, null, messageSupplier); | ||
} | ||
|
||
@Override | ||
protected void logTransactionBegun( | ||
String callerClassName, | ||
String callerMethodName, | ||
String transactionId, | ||
Level level, | ||
Supplier<String> messageSupplier) { | ||
log( | ||
LogKind.LOCAL_TRANSACTION.fullName(), | ||
level, | ||
callerClassName, | ||
callerMethodName, | ||
null, | ||
messageSupplier); | ||
} | ||
|
||
@Override | ||
protected void logTransactionEnded( | ||
String callerClassName, | ||
String callerMethodName, | ||
String transactionId, | ||
Level level, | ||
Supplier<String> messageSupplier) { | ||
log( | ||
LogKind.LOCAL_TRANSACTION.fullName(), | ||
level, | ||
callerClassName, | ||
callerMethodName, | ||
null, | ||
messageSupplier); | ||
} | ||
|
||
@Override | ||
protected void logTransactionSavepointCreated( | ||
String callerClassName, | ||
String callerMethodName, | ||
String transactionId, | ||
String savepointName, | ||
Level level, | ||
Supplier<String> messageSupplier) { | ||
log( | ||
LogKind.LOCAL_TRANSACTION.fullName(), | ||
level, | ||
callerClassName, | ||
callerMethodName, | ||
null, | ||
messageSupplier); | ||
} | ||
|
||
@Override | ||
protected void logTransactionCommitted( | ||
String callerClassName, | ||
String callerMethodName, | ||
String transactionId, | ||
Level level, | ||
Supplier<String> messageSupplier) { | ||
log( | ||
LogKind.LOCAL_TRANSACTION.fullName(), | ||
level, | ||
callerClassName, | ||
callerMethodName, | ||
null, | ||
messageSupplier); | ||
} | ||
|
||
@Override | ||
protected void logTransactionRolledback( | ||
String callerClassName, | ||
String callerMethodName, | ||
String transactionId, | ||
Level level, | ||
Supplier<String> messageSupplier) { | ||
log( | ||
LogKind.LOCAL_TRANSACTION.fullName(), | ||
level, | ||
callerClassName, | ||
callerMethodName, | ||
null, | ||
messageSupplier); | ||
} | ||
|
||
@Override | ||
protected void logTransactionSavepointRolledback( | ||
String callerClassName, | ||
String callerMethodName, | ||
String transactionId, | ||
String savepointName, | ||
Level level, | ||
Supplier<String> messageSupplier) { | ||
log( | ||
LogKind.LOCAL_TRANSACTION.fullName(), | ||
level, | ||
callerClassName, | ||
callerMethodName, | ||
null, | ||
messageSupplier); | ||
} | ||
|
||
@Override | ||
protected void logTransactionSavepointReleased( | ||
String callerClassName, | ||
String callerMethodName, | ||
String transactionId, | ||
String savepointName, | ||
Level level, | ||
Supplier<String> messageSupplier) { | ||
log( | ||
LogKind.LOCAL_TRANSACTION.fullName(), | ||
level, | ||
callerClassName, | ||
callerMethodName, | ||
null, | ||
messageSupplier); | ||
} | ||
|
||
@Override | ||
protected void logTransactionRollbackFailure( | ||
String callerClassName, | ||
String callerMethodName, | ||
SQLException e, | ||
Level level, | ||
Supplier<String> messageSupplier) { | ||
log(LogKind.FAILURE.fullName(), level, callerClassName, callerMethodName, e, messageSupplier); | ||
} | ||
|
||
@Override | ||
protected void logAutoCommitEnablingFailure( | ||
String callerClassName, | ||
String callerMethodName, | ||
SQLException e, | ||
Level level, | ||
Supplier<String> messageSupplier) { | ||
log(LogKind.FAILURE.fullName(), level, callerClassName, callerMethodName, e, messageSupplier); | ||
} | ||
|
||
@Override | ||
protected void logTransactionIsolationSettingFailure( | ||
String callerClassName, | ||
String callerMethodName, | ||
int transactionIsolationLevel, | ||
SQLException e, | ||
Level level, | ||
Supplier<String> messageSupplier) { | ||
log(LogKind.FAILURE.fullName(), level, callerClassName, callerMethodName, e, messageSupplier); | ||
} | ||
|
||
@Override | ||
protected void logConnectionClosingFailure( | ||
String callerClassName, | ||
String callerMethodName, | ||
SQLException e, | ||
Level level, | ||
Supplier<String> messageSupplier) { | ||
log(LogKind.FAILURE.fullName(), level, callerClassName, callerMethodName, e, messageSupplier); | ||
} | ||
|
||
@Override | ||
protected void logStatementClosingFailure( | ||
String callerClassName, | ||
String callerMethodName, | ||
SQLException e, | ||
Level level, | ||
Supplier<String> messageSupplier) { | ||
log(LogKind.FAILURE.fullName(), level, callerClassName, callerMethodName, e, messageSupplier); | ||
} | ||
|
||
@Override | ||
protected void logResultSetClosingFailure( | ||
String callerClassName, | ||
String callerMethodName, | ||
SQLException e, | ||
Level level, | ||
Supplier<String> messageSupplier) { | ||
log(LogKind.FAILURE.fullName(), level, callerClassName, callerMethodName, e, messageSupplier); | ||
} | ||
|
||
protected void log( | ||
String loggerName, | ||
Level level, | ||
String callerClassName, | ||
String callerMethodName, | ||
Throwable throwable, | ||
Supplier<String> messageSupplier) { | ||
Objects.requireNonNull(loggerName); | ||
Objects.requireNonNull(level); | ||
Objects.requireNonNull(messageSupplier); | ||
final Logger logger = LoggerFactory.getLogger(loggerName); | ||
switch (level) { | ||
case ERROR: | ||
if (logger.isErrorEnabled()) { | ||
Marker marker = getMarker(callerClassName, callerMethodName); | ||
logger.error(marker, messageSupplier.get(), throwable); | ||
} | ||
break; | ||
case WARN: | ||
if (logger.isWarnEnabled()) { | ||
Marker marker = getMarker(callerClassName, callerMethodName); | ||
logger.warn(marker, messageSupplier.get(), throwable); | ||
} | ||
break; | ||
case INFO: | ||
if (logger.isInfoEnabled()) { | ||
Marker marker = getMarker(callerClassName, callerMethodName); | ||
logger.info(marker, messageSupplier.get(), throwable); | ||
} | ||
break; | ||
case DEBUG: | ||
if (logger.isDebugEnabled()) { | ||
Marker marker = getMarker(callerClassName, callerMethodName); | ||
logger.debug(marker, messageSupplier.get(), throwable); | ||
} | ||
break; | ||
case TRACE: | ||
if (logger.isTraceEnabled()) { | ||
Marker marker = getMarker(callerClassName, callerMethodName); | ||
logger.trace(marker, messageSupplier.get(), throwable); | ||
} | ||
break; | ||
} | ||
} | ||
|
||
private Marker getMarker(String callerClassName, String callerMethodName) { | ||
return MarkerFactory.getMarker(callerClassName + "#" + callerMethodName); | ||
} | ||
|
||
@Override | ||
protected void log( | ||
Level level, | ||
String callerClassName, | ||
String callerMethodName, | ||
Throwable throwable, | ||
Supplier<String> messageSupplier) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
Oops, something went wrong.