|
| 1 | +package org.seasar.doma.jdbc; |
| 2 | + |
| 3 | +import java.sql.SQLException; |
| 4 | +import java.util.Objects; |
| 5 | +import java.util.function.Supplier; |
| 6 | +import org.slf4j.Logger; |
| 7 | +import org.slf4j.LoggerFactory; |
| 8 | +import org.slf4j.Marker; |
| 9 | +import org.slf4j.MarkerFactory; |
| 10 | +import org.slf4j.event.Level; |
| 11 | + |
| 12 | +public class Slf4jJdbcLogger extends AbstractJdbcLogger<Level> { |
| 13 | + |
| 14 | + public Slf4jJdbcLogger() { |
| 15 | + super(Level.DEBUG); |
| 16 | + } |
| 17 | + |
| 18 | + public Slf4jJdbcLogger(Level level) { |
| 19 | + super(level); |
| 20 | + } |
| 21 | + |
| 22 | + @SuppressWarnings("CStyleArrayDeclaration") |
| 23 | + @Override |
| 24 | + protected void logDaoMethodEntering( |
| 25 | + String callerClassName, |
| 26 | + String callerMethodName, |
| 27 | + Object args[], |
| 28 | + Level level, |
| 29 | + Supplier<String> messageSupplier) { |
| 30 | + log(LogKind.DAO.fullName(), level, callerClassName, callerMethodName, null, messageSupplier); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + protected void logDaoMethodExiting( |
| 35 | + String callerClassName, |
| 36 | + String callerMethodName, |
| 37 | + Object result, |
| 38 | + Level level, |
| 39 | + Supplier<String> messageSupplier) { |
| 40 | + log(LogKind.DAO.fullName(), level, callerClassName, callerMethodName, null, messageSupplier); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + protected void logDaoMethodThrowing( |
| 45 | + String callerClassName, |
| 46 | + String callerMethodName, |
| 47 | + RuntimeException e, |
| 48 | + Level level, |
| 49 | + Supplier<String> messageSupplier) { |
| 50 | + log(LogKind.DAO.fullName(), level, callerClassName, callerMethodName, null, messageSupplier); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + protected void logSqlExecutionSkipping( |
| 55 | + String callerClassName, |
| 56 | + String callerMethodName, |
| 57 | + SqlExecutionSkipCause cause, |
| 58 | + Level level, |
| 59 | + Supplier<String> messageSupplier) { |
| 60 | + String loggerName = LogKind.SKIP.fullName() + "." + cause.name(); |
| 61 | + log(loggerName, level, callerClassName, callerMethodName, null, messageSupplier); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + protected void logSql( |
| 66 | + String callerClassName, |
| 67 | + String callerMethodName, |
| 68 | + Sql<?> sql, |
| 69 | + Level level, |
| 70 | + Supplier<String> messageSupplier) { |
| 71 | + String loggerName = LogKind.SQL.fullName() + "." + sql.getKind().name(); |
| 72 | + log(loggerName, level, callerClassName, callerMethodName, null, messageSupplier); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + protected void logTransactionBegun( |
| 77 | + String callerClassName, |
| 78 | + String callerMethodName, |
| 79 | + String transactionId, |
| 80 | + Level level, |
| 81 | + Supplier<String> messageSupplier) { |
| 82 | + log( |
| 83 | + LogKind.LOCAL_TRANSACTION.fullName(), |
| 84 | + level, |
| 85 | + callerClassName, |
| 86 | + callerMethodName, |
| 87 | + null, |
| 88 | + messageSupplier); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + protected void logTransactionEnded( |
| 93 | + String callerClassName, |
| 94 | + String callerMethodName, |
| 95 | + String transactionId, |
| 96 | + Level level, |
| 97 | + Supplier<String> messageSupplier) { |
| 98 | + log( |
| 99 | + LogKind.LOCAL_TRANSACTION.fullName(), |
| 100 | + level, |
| 101 | + callerClassName, |
| 102 | + callerMethodName, |
| 103 | + null, |
| 104 | + messageSupplier); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + protected void logTransactionSavepointCreated( |
| 109 | + String callerClassName, |
| 110 | + String callerMethodName, |
| 111 | + String transactionId, |
| 112 | + String savepointName, |
| 113 | + Level level, |
| 114 | + Supplier<String> messageSupplier) { |
| 115 | + log( |
| 116 | + LogKind.LOCAL_TRANSACTION.fullName(), |
| 117 | + level, |
| 118 | + callerClassName, |
| 119 | + callerMethodName, |
| 120 | + null, |
| 121 | + messageSupplier); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + protected void logTransactionCommitted( |
| 126 | + String callerClassName, |
| 127 | + String callerMethodName, |
| 128 | + String transactionId, |
| 129 | + Level level, |
| 130 | + Supplier<String> messageSupplier) { |
| 131 | + log( |
| 132 | + LogKind.LOCAL_TRANSACTION.fullName(), |
| 133 | + level, |
| 134 | + callerClassName, |
| 135 | + callerMethodName, |
| 136 | + null, |
| 137 | + messageSupplier); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + protected void logTransactionRolledback( |
| 142 | + String callerClassName, |
| 143 | + String callerMethodName, |
| 144 | + String transactionId, |
| 145 | + Level level, |
| 146 | + Supplier<String> messageSupplier) { |
| 147 | + log( |
| 148 | + LogKind.LOCAL_TRANSACTION.fullName(), |
| 149 | + level, |
| 150 | + callerClassName, |
| 151 | + callerMethodName, |
| 152 | + null, |
| 153 | + messageSupplier); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + protected void logTransactionSavepointRolledback( |
| 158 | + String callerClassName, |
| 159 | + String callerMethodName, |
| 160 | + String transactionId, |
| 161 | + String savepointName, |
| 162 | + Level level, |
| 163 | + Supplier<String> messageSupplier) { |
| 164 | + log( |
| 165 | + LogKind.LOCAL_TRANSACTION.fullName(), |
| 166 | + level, |
| 167 | + callerClassName, |
| 168 | + callerMethodName, |
| 169 | + null, |
| 170 | + messageSupplier); |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + protected void logTransactionSavepointReleased( |
| 175 | + String callerClassName, |
| 176 | + String callerMethodName, |
| 177 | + String transactionId, |
| 178 | + String savepointName, |
| 179 | + Level level, |
| 180 | + Supplier<String> messageSupplier) { |
| 181 | + log( |
| 182 | + LogKind.LOCAL_TRANSACTION.fullName(), |
| 183 | + level, |
| 184 | + callerClassName, |
| 185 | + callerMethodName, |
| 186 | + null, |
| 187 | + messageSupplier); |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + protected void logTransactionRollbackFailure( |
| 192 | + String callerClassName, |
| 193 | + String callerMethodName, |
| 194 | + SQLException e, |
| 195 | + Level level, |
| 196 | + Supplier<String> messageSupplier) { |
| 197 | + log(LogKind.FAILURE.fullName(), level, callerClassName, callerMethodName, e, messageSupplier); |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + protected void logAutoCommitEnablingFailure( |
| 202 | + String callerClassName, |
| 203 | + String callerMethodName, |
| 204 | + SQLException e, |
| 205 | + Level level, |
| 206 | + Supplier<String> messageSupplier) { |
| 207 | + log(LogKind.FAILURE.fullName(), level, callerClassName, callerMethodName, e, messageSupplier); |
| 208 | + } |
| 209 | + |
| 210 | + @Override |
| 211 | + protected void logTransactionIsolationSettingFailure( |
| 212 | + String callerClassName, |
| 213 | + String callerMethodName, |
| 214 | + int transactionIsolationLevel, |
| 215 | + SQLException e, |
| 216 | + Level level, |
| 217 | + Supplier<String> messageSupplier) { |
| 218 | + log(LogKind.FAILURE.fullName(), level, callerClassName, callerMethodName, e, messageSupplier); |
| 219 | + } |
| 220 | + |
| 221 | + @Override |
| 222 | + protected void logConnectionClosingFailure( |
| 223 | + String callerClassName, |
| 224 | + String callerMethodName, |
| 225 | + SQLException e, |
| 226 | + Level level, |
| 227 | + Supplier<String> messageSupplier) { |
| 228 | + log(LogKind.FAILURE.fullName(), level, callerClassName, callerMethodName, e, messageSupplier); |
| 229 | + } |
| 230 | + |
| 231 | + @Override |
| 232 | + protected void logStatementClosingFailure( |
| 233 | + String callerClassName, |
| 234 | + String callerMethodName, |
| 235 | + SQLException e, |
| 236 | + Level level, |
| 237 | + Supplier<String> messageSupplier) { |
| 238 | + log(LogKind.FAILURE.fullName(), level, callerClassName, callerMethodName, e, messageSupplier); |
| 239 | + } |
| 240 | + |
| 241 | + @Override |
| 242 | + protected void logResultSetClosingFailure( |
| 243 | + String callerClassName, |
| 244 | + String callerMethodName, |
| 245 | + SQLException e, |
| 246 | + Level level, |
| 247 | + Supplier<String> messageSupplier) { |
| 248 | + log(LogKind.FAILURE.fullName(), level, callerClassName, callerMethodName, e, messageSupplier); |
| 249 | + } |
| 250 | + |
| 251 | + protected void log( |
| 252 | + String loggerName, |
| 253 | + Level level, |
| 254 | + String callerClassName, |
| 255 | + String callerMethodName, |
| 256 | + Throwable throwable, |
| 257 | + Supplier<String> messageSupplier) { |
| 258 | + Objects.requireNonNull(loggerName); |
| 259 | + Objects.requireNonNull(level); |
| 260 | + Objects.requireNonNull(messageSupplier); |
| 261 | + final Logger logger = LoggerFactory.getLogger(loggerName); |
| 262 | + switch (level) { |
| 263 | + case ERROR: |
| 264 | + if (logger.isErrorEnabled()) { |
| 265 | + Marker marker = getMarker(callerClassName, callerMethodName); |
| 266 | + logger.error(marker, messageSupplier.get(), throwable); |
| 267 | + } |
| 268 | + break; |
| 269 | + case WARN: |
| 270 | + if (logger.isWarnEnabled()) { |
| 271 | + Marker marker = getMarker(callerClassName, callerMethodName); |
| 272 | + logger.warn(marker, messageSupplier.get(), throwable); |
| 273 | + } |
| 274 | + break; |
| 275 | + case INFO: |
| 276 | + if (logger.isInfoEnabled()) { |
| 277 | + Marker marker = getMarker(callerClassName, callerMethodName); |
| 278 | + logger.info(marker, messageSupplier.get(), throwable); |
| 279 | + } |
| 280 | + break; |
| 281 | + case DEBUG: |
| 282 | + if (logger.isDebugEnabled()) { |
| 283 | + Marker marker = getMarker(callerClassName, callerMethodName); |
| 284 | + logger.debug(marker, messageSupplier.get(), throwable); |
| 285 | + } |
| 286 | + break; |
| 287 | + case TRACE: |
| 288 | + if (logger.isTraceEnabled()) { |
| 289 | + Marker marker = getMarker(callerClassName, callerMethodName); |
| 290 | + logger.trace(marker, messageSupplier.get(), throwable); |
| 291 | + } |
| 292 | + break; |
| 293 | + } |
| 294 | + } |
| 295 | + |
| 296 | + private Marker getMarker(String callerClassName, String callerMethodName) { |
| 297 | + return MarkerFactory.getMarker(callerClassName + "#" + callerMethodName); |
| 298 | + } |
| 299 | + |
| 300 | + @Override |
| 301 | + protected void log( |
| 302 | + Level level, |
| 303 | + String callerClassName, |
| 304 | + String callerMethodName, |
| 305 | + Throwable throwable, |
| 306 | + Supplier<String> messageSupplier) { |
| 307 | + throw new UnsupportedOperationException(); |
| 308 | + } |
| 309 | +} |
0 commit comments