4343import java .util .concurrent .TimeUnit ;
4444
4545public final class IndexingSlowLog implements IndexingOperationListener {
46- private final Index index ;
47- private boolean reformat ;
48- private long indexWarnThreshold ;
49- private long indexInfoThreshold ;
50- private long indexDebugThreshold ;
51- private long indexTraceThreshold ;
52- /**
53- * How much of the source to log in the slowlog - 0 means log none and
54- * anything greater than 0 means log at least that many <em>characters</em>
55- * of the source.
56- */
57- private int maxSourceCharsToLog ;
58-
59- private final Logger indexLogger ;
60-
61- private static final String INDEX_INDEXING_SLOWLOG_PREFIX = "index.indexing.slowlog" ;
46+ public static final String INDEX_INDEXING_SLOWLOG_PREFIX = "index.indexing.slowlog" ;
6247 public static final Setting <TimeValue > INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_WARN_SETTING =
6348 Setting .timeSetting (INDEX_INDEXING_SLOWLOG_PREFIX +".threshold.index.warn" , TimeValue .timeValueNanos (-1 ),
6449 TimeValue .timeValueMillis (-1 ), Property .Dynamic , Property .IndexScope );
@@ -76,6 +61,22 @@ public final class IndexingSlowLog implements IndexingOperationListener {
7661 public static final Setting <SlowLogLevel > INDEX_INDEXING_SLOWLOG_LEVEL_SETTING =
7762 new Setting <>(INDEX_INDEXING_SLOWLOG_PREFIX +".level" , SlowLogLevel .TRACE .name (), SlowLogLevel ::parse , Property .Dynamic ,
7863 Property .IndexScope );
64+
65+ private final Logger indexLogger ;
66+ private final Index index ;
67+
68+ private boolean reformat ;
69+ private long indexWarnThreshold ;
70+ private long indexInfoThreshold ;
71+ private long indexDebugThreshold ;
72+ private long indexTraceThreshold ;
73+ /*
74+ * How much of the source to log in the slowlog - 0 means log none and anything greater than 0 means log at least that many
75+ * <em>characters</em> of the source.
76+ */
77+ private int maxSourceCharsToLog ;
78+ private SlowLogLevel level ;
79+
7980 /**
8081 * Reads how much of the source to log. The user can specify any value they
8182 * like and numbers are interpreted the maximum number of characters to log
@@ -92,7 +93,8 @@ public final class IndexingSlowLog implements IndexingOperationListener {
9293 }, Property .Dynamic , Property .IndexScope );
9394
9495 IndexingSlowLog (IndexSettings indexSettings ) {
95- this .indexLogger = LogManager .getLogger (INDEX_INDEXING_SLOWLOG_PREFIX + ".index." + indexSettings .getUUID ());
96+ this .indexLogger = LogManager .getLogger (INDEX_INDEXING_SLOWLOG_PREFIX + ".index" );
97+ Loggers .setLevel (this .indexLogger , SlowLogLevel .TRACE .name ());
9698 this .index = indexSettings .getIndex ();
9799
98100 indexSettings .getScopedSettings ().addSettingsUpdateConsumer (INDEX_INDEXING_SLOWLOG_REFORMAT_SETTING , this ::setReformat );
@@ -121,7 +123,7 @@ private void setMaxSourceCharsToLog(int maxSourceCharsToLog) {
121123 }
122124
123125 private void setLevel (SlowLogLevel level ) {
124- Loggers . setLevel ( this .indexLogger , level . name ()) ;
126+ this .level = level ;
125127 }
126128
127129 private void setWarnThreshold (TimeValue warnThreshold ) {
@@ -149,13 +151,14 @@ public void postIndex(ShardId shardId, Engine.Index indexOperation, Engine.Index
149151 if (result .getResultType () == Engine .Result .Type .SUCCESS ) {
150152 final ParsedDocument doc = indexOperation .parsedDoc ();
151153 final long tookInNanos = result .getTook ();
152- if (indexWarnThreshold >= 0 && tookInNanos > indexWarnThreshold ) {
154+ // when logger level is more specific than WARN AND event is within threshold it should be logged
155+ if (indexWarnThreshold >= 0 && tookInNanos > indexWarnThreshold && level .isLevelEnabledFor (SlowLogLevel .WARN )) {
153156 indexLogger .warn ( new IndexingSlowLogMessage (index , doc , tookInNanos , reformat , maxSourceCharsToLog ));
154- } else if (indexInfoThreshold >= 0 && tookInNanos > indexInfoThreshold ) {
157+ } else if (indexInfoThreshold >= 0 && tookInNanos > indexInfoThreshold && level . isLevelEnabledFor ( SlowLogLevel . INFO ) ) {
155158 indexLogger .info (new IndexingSlowLogMessage (index , doc , tookInNanos , reformat , maxSourceCharsToLog ));
156- } else if (indexDebugThreshold >= 0 && tookInNanos > indexDebugThreshold ) {
159+ } else if (indexDebugThreshold >= 0 && tookInNanos > indexDebugThreshold && level . isLevelEnabledFor ( SlowLogLevel . DEBUG ) ) {
157160 indexLogger .debug (new IndexingSlowLogMessage (index , doc , tookInNanos , reformat , maxSourceCharsToLog ));
158- } else if (indexTraceThreshold >= 0 && tookInNanos > indexTraceThreshold ) {
161+ } else if (indexTraceThreshold >= 0 && tookInNanos > indexTraceThreshold && level . isLevelEnabledFor ( SlowLogLevel . TRACE ) ) {
159162 indexLogger .trace ( new IndexingSlowLogMessage (index , doc , tookInNanos , reformat , maxSourceCharsToLog ));
160163 }
161164 }
@@ -258,7 +261,7 @@ int getMaxSourceCharsToLog() {
258261 }
259262
260263 SlowLogLevel getLevel () {
261- return SlowLogLevel . parse ( indexLogger . getLevel (). name ()) ;
264+ return level ;
262265 }
263266
264267}
0 commit comments