Skip to content

Commit

Permalink
Enable per-SQL-ID log suppression (#322) (#332)
Browse files Browse the repository at this point in the history
* 各Logger用のインタフェースを作成し必要なクラスがimplementsするように修正。そのうえでSLF4J 2.xのAPIを利用するように変更(性能改善)

* Changed to use slf4j v2.0 API
Also added suppressLogging API

* Refactoring log-related class and method names.
  • Loading branch information
HidekiSugimoto189 authored Sep 12, 2024
1 parent 336d7e4 commit 0bca727
Show file tree
Hide file tree
Showing 41 changed files with 968 additions and 619 deletions.
363 changes: 184 additions & 179 deletions src/main/java/jp/co/future/uroborosql/SqlAgentImpl.java

Large diffs are not rendered by default.

30 changes: 11 additions & 19 deletions src/main/java/jp/co/future/uroborosql/SqlEntityQueryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jp.co.future.uroborosql.context.ExecutionContext;
import jp.co.future.uroborosql.dialect.Dialect;
import jp.co.future.uroborosql.enums.ForUpdateType;
Expand All @@ -26,6 +23,7 @@
import jp.co.future.uroborosql.exception.EntitySqlRuntimeException;
import jp.co.future.uroborosql.exception.UroborosqlRuntimeException;
import jp.co.future.uroborosql.fluent.SqlEntityQuery;
import jp.co.future.uroborosql.log.support.ServiceLoggingSupport;
import jp.co.future.uroborosql.mapping.EntityHandler;
import jp.co.future.uroborosql.mapping.MappingUtils;
import jp.co.future.uroborosql.mapping.TableMetadata;
Expand All @@ -38,10 +36,8 @@
* @param <E> Entity型
* @author ota
*/
final class SqlEntityQueryImpl<E> extends AbstractExtractionCondition<SqlEntityQuery<E>> implements SqlEntityQuery<E> {
/** ロガー */
private static final Logger LOG = LoggerFactory.getLogger("jp.co.future.uroborosql.log");

final class SqlEntityQueryImpl<E> extends AbstractExtractionCondition<SqlEntityQuery<E>>
implements SqlEntityQuery<E>, ServiceLoggingSupport {
private final EntityHandler<?> entityHandler;
private final Class<? extends E> entityType;
private final List<SortOrder> sortOrders;
Expand Down Expand Up @@ -585,9 +581,8 @@ public SqlEntityQuery<E> forUpdateNoWait() {
return this;
} else if (!agent().getSqlConfig().getSqlAgentProvider().isStrictForUpdateType()
&& dialect.supportsForUpdate()) {
if (LOG.isWarnEnabled()) {
LOG.warn("'FOR UPDATE NOWAIT' is not supported. Set 'FOR UPDATE' instead.");
}
warnWith(LOG)
.log("'FOR UPDATE NOWAIT' is not supported. Set 'FOR UPDATE' instead.");
this.forUpdateType = ForUpdateType.NORMAL;
return this;
} else {
Expand All @@ -606,9 +601,8 @@ public SqlEntityQuery<E> forUpdateWait() {
return forUpdateWait(agent().getSqlConfig().getSqlAgentProvider().getDefaultForUpdateWaitSeconds());
} else if (!agent().getSqlConfig().getSqlAgentProvider().isStrictForUpdateType()
&& dialect.supportsForUpdate()) {
if (LOG.isWarnEnabled()) {
LOG.warn("'FOR UPDATE WAIT' is not supported. Set 'FOR UPDATE' instead.");
}
warnWith(LOG)
.log("'FOR UPDATE WAIT' is not supported. Set 'FOR UPDATE' instead.");
this.forUpdateType = ForUpdateType.NORMAL;
return this;
} else {
Expand All @@ -629,9 +623,8 @@ public SqlEntityQuery<E> forUpdateWait(final int waitSeconds) {
return this;
} else if (!agent().getSqlConfig().getSqlAgentProvider().isStrictForUpdateType()
&& dialect.supportsForUpdate()) {
if (LOG.isWarnEnabled()) {
LOG.warn("'FOR UPDATE WAIT' is not supported. Set 'FOR UPDATE' instead.");
}
warnWith(LOG)
.log("'FOR UPDATE WAIT' is not supported. Set 'FOR UPDATE' instead.");
this.forUpdateType = ForUpdateType.NORMAL;
return this;
} else {
Expand All @@ -649,9 +642,8 @@ public SqlEntityQuery<E> hint(final String hint) {
if (dialect.supportsOptimizerHints()) {
this.optimizerHints.add(hint);
} else {
if (LOG.isWarnEnabled()) {
LOG.warn("Optimizer Hints is not supported.");
}
warnWith(LOG)
.log("Optimizer Hints is not supported.");
}
return this;
}
Expand Down
40 changes: 18 additions & 22 deletions src/main/java/jp/co/future/uroborosql/UroboroSQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jp.co.future.uroborosql.config.SqlConfig;
import jp.co.future.uroborosql.connection.ConnectionContext;
import jp.co.future.uroborosql.connection.ConnectionContextBuilder;
Expand All @@ -31,6 +28,8 @@
import jp.co.future.uroborosql.event.EventListenerHolder;
import jp.co.future.uroborosql.expr.ExpressionParser;
import jp.co.future.uroborosql.expr.ExpressionParserFactory;
import jp.co.future.uroborosql.log.support.ServiceLoggingSupport;
import jp.co.future.uroborosql.log.support.SettingLoggingSupport;
import jp.co.future.uroborosql.mapping.DefaultEntityHandler;
import jp.co.future.uroborosql.mapping.EntityHandler;
import jp.co.future.uroborosql.store.SqlResourceManager;
Expand All @@ -42,10 +41,7 @@
* @author H.Sugimoto
* @since v0.4.0
*/
public final class UroboroSQL {
/** 設定ロガー */
private static final Logger SETTING_LOG = LoggerFactory.getLogger("jp.co.future.uroborosql.setting");

public final class UroboroSQL implements ServiceLoggingSupport, SettingLoggingSupport {
private UroboroSQL() {
}

Expand Down Expand Up @@ -238,7 +234,7 @@ public SqlConfig build() {

}

public static final class InternalConfig implements SqlConfig {
public static final class InternalConfig implements SqlConfig, SettingLoggingSupport {
/**
* コネクション提供クラス.
*/
Expand Down Expand Up @@ -300,15 +296,15 @@ public static final class InternalConfig implements SqlConfig {
this.entityHandler = new DefaultEntityHandler();
if (clock == null) {
this.clock = Clock.systemDefaultZone();
if (SETTING_LOG.isWarnEnabled()) {
SETTING_LOG.warn("SqlConfig - Clock was not set. Set SystemClock.");
}
warnWith(SETTING_LOG)
.log("SqlConfig - Clock was not set. Set SystemClock.");
} else {
this.clock = clock;
}
if (SETTING_LOG.isInfoEnabled()) {
SETTING_LOG.info("SqlConfig - Clock : {} has been selected.", this.clock.toString());
}
infoWith(SETTING_LOG)
.setMessage("SqlConfig - Clock : {} has been selected.")
.addArgument(this.clock)
.log();

if (dialect == null) {
this.dialect = StreamSupport.stream(ServiceLoader.load(Dialect.class).spliterator(), false)
Expand All @@ -318,10 +314,10 @@ public static final class InternalConfig implements SqlConfig {
} else {
this.dialect = dialect;
}
if (SETTING_LOG.isInfoEnabled()) {
SETTING_LOG.info("SqlConfig - Dialect : {} has been selected.",
this.dialect.getClass().getSimpleName());
}
infoWith(SETTING_LOG)
.setMessage("SqlConfig - Dialect : {} has been selected.")
.addArgument(() -> this.dialect.getClass().getSimpleName())
.log();

if (expressionParser == null) {
var expressionParserFactory = StreamSupport
Expand All @@ -333,10 +329,10 @@ public static final class InternalConfig implements SqlConfig {
} else {
this.expressionParser = expressionParser;
}
if (SETTING_LOG.isInfoEnabled()) {
SETTING_LOG.info("SqlConfig - ExpressionParser : {} has been selected.",
this.expressionParser.getClass().getSimpleName());
}
infoWith(SETTING_LOG)
.setMessage("SqlConfig - ExpressionParser : {} has been selected.")
.addArgument(() -> this.expressionParser.getClass().getSimpleName())
.log();

this.sqlResourceManager.setDialect(this.dialect);
this.executionContextProvider.setSqlConfig(this);
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/jp/co/future/uroborosql/client/SqlREPL.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import jp.co.future.uroborosql.client.completer.TableNameCompleter;
import jp.co.future.uroborosql.config.SqlConfig;
import jp.co.future.uroborosql.event.subscriber.DumpResultEventSubscriber;
import jp.co.future.uroborosql.log.support.ReplLoggingSupport;
import jp.co.future.uroborosql.store.SqlResourceManagerImpl;
import jp.co.future.uroborosql.utils.ObjectUtils;

Expand All @@ -59,10 +60,7 @@
*
* @author H.Sugimoto
*/
public class SqlREPL {
/** REPLロガー */
private static final org.slf4j.Logger REPL_LOG = LoggerFactory.getLogger("jp.co.future.uroborosql.repl");

public class SqlREPL implements ReplLoggingSupport {
/** プロパティ上のクラスパスに指定された環境変数を置換するための正規表現 */
private static final Pattern SYSPROP_PAT = Pattern.compile("\\$\\{(.+?)\\}");
/** プロパティパス */
Expand Down Expand Up @@ -201,7 +199,10 @@ private void initialize(final Terminal terminal) throws Exception {

urls.add(Paths.get(sb.toString()).toUri().toURL());
} catch (Exception ex) {
REPL_LOG.error(ex.getMessage(), ex);
errorWith(REPL_LOG)
.setMessage(ex.getMessage())
.setCause(ex)
.log();
}
});
additionalClassLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]), currentClassLoader);
Expand All @@ -213,7 +214,10 @@ private void initialize(final Terminal terminal) throws Exception {
try {
DriverManager.registerDriver(new DriverShim(driver));
} catch (Exception ex) {
REPL_LOG.error(ex.getMessage(), ex);
errorWith(REPL_LOG)
.setMessage(ex.getMessage())
.setCause(ex)
.log();
}
});

Expand Down Expand Up @@ -307,7 +311,10 @@ private void listen(final Terminal terminal) throws IOException {
} catch (UserInterruptException | EndOfFileException ex) {
break;
} catch (Exception ex) {
REPL_LOG.error(ex.getMessage(), ex);
errorWith(REPL_LOG)
.setMessage(ex.getMessage())
.setCause(ex)
.log();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

import org.jline.reader.LineReader;
import org.jline.terminal.Terminal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jp.co.future.uroborosql.client.completer.TableNameCompleter;
import jp.co.future.uroborosql.config.SqlConfig;
Expand All @@ -30,9 +28,6 @@
* @author H.Sugimoto
*/
public class DescCommand extends ReplCommand {
/** REPLロガー */
private static final Logger REPL_LOG = LoggerFactory.getLogger("jp.co.future.uroborosql.repl");

/** DESCで表示する項目 */
private static final String[] DESC_COLUMN_LABELS = { "TABLE_NAME", "COLUMN_NAME", "TYPE_NAME", "COLUMN_SIZE",
"DECIMAL_DIGITS", "IS_NULLABLE", "COLUMN_DEF", "REMARKS" };
Expand Down Expand Up @@ -130,7 +125,10 @@ public boolean execute(final LineReader reader, final String[] parts, final SqlC
}
writer.println();
} catch (SQLException ex) {
REPL_LOG.error(ex.getMessage(), ex);
errorWith(REPL_LOG)
.setMessage(ex.getMessage())
.setCause(ex)
.log();
}
writer.flush();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

import org.jline.reader.LineReader;
import org.jline.terminal.Terminal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jp.co.future.uroborosql.client.completer.SqlKeywordCompleter;
import jp.co.future.uroborosql.client.completer.SqlKeywordCompleter.SqlKeyword;
Expand All @@ -27,9 +25,6 @@
* @author H.Sugimoto
*/
public class GenerateCommand extends ReplCommand {
/** REPLロガー */
private static final Logger REPL_LOG = LoggerFactory.getLogger("jp.co.future.uroborosql.repl");

/**
* Constructor
*/
Expand Down Expand Up @@ -79,7 +74,10 @@ public boolean execute(final LineReader reader, final String[] parts, final SqlC
}).orElseGet(() -> sqlConfig.getEntityHandler().createSelectContext(agent, metadata, null, true));
writer.println(ctx.getSql());
} catch (SQLException ex) {
REPL_LOG.error(ex.getMessage(), ex);
errorWith(REPL_LOG)
.setMessage(ex.getMessage())
.setCause(ex)
.log();
}

writer.flush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@

import jp.co.future.uroborosql.client.SqlREPL;
import jp.co.future.uroborosql.config.SqlConfig;
import jp.co.future.uroborosql.log.support.ReplLoggingSupport;

/**
* REPLで実行するコマンドの抽象親クラス
*
* @author H.Sugimoto
*/
public abstract class ReplCommand {
public abstract class ReplCommand implements ReplLoggingSupport {
/** 利用する入力補完の並び */
private final List<Class<? extends Completer>> completers;
/** HELPコマンドで非表示 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,18 @@
import org.jline.reader.Candidate;
import org.jline.reader.LineReader;
import org.jline.reader.ParsedLine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jp.co.future.uroborosql.client.command.ReplCommand;
import jp.co.future.uroborosql.connection.ConnectionSupplier;
import jp.co.future.uroborosql.log.support.ReplLoggingSupport;

/**
* テーブル名を補完するCompleter
*
* @author H.Sugimoto
*
*/
public class TableNameCompleter extends AbstractCompleter {
/** REPLロガー */
private static final Logger REPL_LOG = LoggerFactory.getLogger("jp.co.future.uroborosql.repl");
public class TableNameCompleter extends AbstractCompleter implements ReplLoggingSupport {

/** Connectionサプライヤ. */
private final ConnectionSupplier connectionSupplier;
Expand Down Expand Up @@ -91,7 +88,10 @@ public void complete(final LineReader reader, final ParsedLine line, final List<
}
}
} catch (SQLException ex) {
REPL_LOG.error(ex.getMessage(), ex);
errorWith(REPL_LOG)
.setMessage(ex.getMessage())
.setCause(ex)
.log();
return;
}
}
Expand Down
Loading

0 comments on commit 0bca727

Please sign in to comment.