39
39
import java .util .concurrent .TimeUnit ;
40
40
41
41
public final class IndexingSlowLog implements IndexingOperationListener {
42
- private final Index index ;
43
- private boolean reformat ;
44
- private long indexWarnThreshold ;
45
- private long indexInfoThreshold ;
46
- private long indexDebugThreshold ;
47
- private long indexTraceThreshold ;
48
- /**
49
- * How much of the source to log in the slowlog - 0 means log none and
50
- * anything greater than 0 means log at least that many <em>characters</em>
51
- * of the source.
52
- */
53
- private int maxSourceCharsToLog ;
54
-
55
- private final Logger indexLogger ;
56
-
57
- private static final String INDEX_INDEXING_SLOWLOG_PREFIX = "index.indexing.slowlog" ;
42
+ public static final String INDEX_INDEXING_SLOWLOG_PREFIX = "index.indexing.slowlog" ;
58
43
public static final Setting <TimeValue > INDEX_INDEXING_SLOWLOG_THRESHOLD_INDEX_WARN_SETTING =
59
44
Setting .timeSetting (INDEX_INDEXING_SLOWLOG_PREFIX +".threshold.index.warn" , TimeValue .timeValueNanos (-1 ),
60
45
TimeValue .timeValueMillis (-1 ), Property .Dynamic , Property .IndexScope );
@@ -72,6 +57,22 @@ public final class IndexingSlowLog implements IndexingOperationListener {
72
57
public static final Setting <SlowLogLevel > INDEX_INDEXING_SLOWLOG_LEVEL_SETTING =
73
58
new Setting <>(INDEX_INDEXING_SLOWLOG_PREFIX +".level" , SlowLogLevel .TRACE .name (), SlowLogLevel ::parse , Property .Dynamic ,
74
59
Property .IndexScope );
60
+
61
+ private final Logger indexLogger ;
62
+ private final Index index ;
63
+
64
+ private boolean reformat ;
65
+ private long indexWarnThreshold ;
66
+ private long indexInfoThreshold ;
67
+ private long indexDebugThreshold ;
68
+ private long indexTraceThreshold ;
69
+ /*
70
+ * 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
71
+ * <em>characters</em> of the source.
72
+ */
73
+ private int maxSourceCharsToLog ;
74
+ private SlowLogLevel level ;
75
+
75
76
/**
76
77
* Reads how much of the source to log. The user can specify any value they
77
78
* like and numbers are interpreted the maximum number of characters to log
@@ -88,7 +89,8 @@ public final class IndexingSlowLog implements IndexingOperationListener {
88
89
}, Property .Dynamic , Property .IndexScope );
89
90
90
91
IndexingSlowLog (IndexSettings indexSettings ) {
91
- this .indexLogger = LogManager .getLogger (INDEX_INDEXING_SLOWLOG_PREFIX + ".index." + indexSettings .getUUID ());
92
+ this .indexLogger = LogManager .getLogger (INDEX_INDEXING_SLOWLOG_PREFIX + ".index" );
93
+ Loggers .setLevel (this .indexLogger , SlowLogLevel .TRACE .name ());
92
94
this .index = indexSettings .getIndex ();
93
95
94
96
indexSettings .getScopedSettings ().addSettingsUpdateConsumer (INDEX_INDEXING_SLOWLOG_REFORMAT_SETTING , this ::setReformat );
@@ -117,7 +119,7 @@ private void setMaxSourceCharsToLog(int maxSourceCharsToLog) {
117
119
}
118
120
119
121
private void setLevel (SlowLogLevel level ) {
120
- Loggers . setLevel ( this .indexLogger , level . name ()) ;
122
+ this .level = level ;
121
123
}
122
124
123
125
private void setWarnThreshold (TimeValue warnThreshold ) {
@@ -145,13 +147,14 @@ public void postIndex(ShardId shardId, Engine.Index indexOperation, Engine.Index
145
147
if (result .getResultType () == Engine .Result .Type .SUCCESS ) {
146
148
final ParsedDocument doc = indexOperation .parsedDoc ();
147
149
final long tookInNanos = result .getTook ();
148
- if (indexWarnThreshold >= 0 && tookInNanos > indexWarnThreshold ) {
150
+ // when logger level is more specific than WARN AND event is within threshold it should be logged
151
+ if (indexWarnThreshold >= 0 && tookInNanos > indexWarnThreshold && level .isLevelEnabledFor (SlowLogLevel .WARN )) {
149
152
indexLogger .warn ("{}" , new SlowLogParsedDocumentPrinter (index , doc , tookInNanos , reformat , maxSourceCharsToLog ));
150
- } else if (indexInfoThreshold >= 0 && tookInNanos > indexInfoThreshold ) {
153
+ } else if (indexInfoThreshold >= 0 && tookInNanos > indexInfoThreshold && level . isLevelEnabledFor ( SlowLogLevel . INFO ) ) {
151
154
indexLogger .info ("{}" , new SlowLogParsedDocumentPrinter (index , doc , tookInNanos , reformat , maxSourceCharsToLog ));
152
- } else if (indexDebugThreshold >= 0 && tookInNanos > indexDebugThreshold ) {
155
+ } else if (indexDebugThreshold >= 0 && tookInNanos > indexDebugThreshold && level . isLevelEnabledFor ( SlowLogLevel . DEBUG ) ) {
153
156
indexLogger .debug ("{}" , new SlowLogParsedDocumentPrinter (index , doc , tookInNanos , reformat , maxSourceCharsToLog ));
154
- } else if (indexTraceThreshold >= 0 && tookInNanos > indexTraceThreshold ) {
157
+ } else if (indexTraceThreshold >= 0 && tookInNanos > indexTraceThreshold && level . isLevelEnabledFor ( SlowLogLevel . TRACE ) ) {
155
158
indexLogger .trace ("{}" , new SlowLogParsedDocumentPrinter (index , doc , tookInNanos , reformat , maxSourceCharsToLog ));
156
159
}
157
160
}
@@ -230,7 +233,7 @@ int getMaxSourceCharsToLog() {
230
233
}
231
234
232
235
SlowLogLevel getLevel () {
233
- return SlowLogLevel . parse ( indexLogger . getLevel (). name ()) ;
236
+ return level ;
234
237
}
235
238
236
239
}
0 commit comments