Skip to content

Commit a0681b7

Browse files
committed
fix SLF4J-499
Signed-off-by: Ceki Gulcu <[email protected]>
1 parent c89dc3d commit a0681b7

File tree

3 files changed

+68
-15
lines changed

3 files changed

+68
-15
lines changed

slf4j-simple/src/main/java/org/slf4j/impl/SimpleLogger.java

+29-14
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,20 @@
5656
* </li>
5757
*
5858
* <li><code>org.slf4j.simpleLogger.defaultLogLevel</code> - Default log level
59-
* for all instances of SimpleLogger. Must be one of ("trace", "debug", "info",
60-
* "warn", "error" or "off"). If not specified, defaults to "info".</li>
59+
* for all instances of SimpleLogger. Must be one of "trace", "debug", "info",
60+
* "warn", "error" or "off". If not specified, defaults to "info".</li>
6161
*
62-
* <li><code>org.slf4j.simpleLogger.log.<em>a.b.c</em></code> - Logging detail
63-
* level for a SimpleLogger instance named "a.b.c". Right-side value must be one
62+
* <li><code>org.slf4j.simpleLogger.log.<em>com.foo.bar</em></code> - Logging detail
63+
* level for a SimpleLogger instance named "com.foo.bar". Right-side value must be one
6464
* of "trace", "debug", "info", "warn", "error" or "off". When a SimpleLogger
6565
* named "a.b.c" is initialized, its level is assigned from this property. If
6666
* unspecified, the level of nearest parent logger will be used, and if none is
6767
* set, then the value specified by
6868
* <code>org.slf4j.simpleLogger.defaultLogLevel</code> will be used.</li>
6969
*
70-
* <li><code>org.slf4j.simpleLogger.showDateTime</code> - Set to
71-
* <code>true</code> if you want the current date and time to be included in
72-
* output messages. Default is <code>false</code></li>
70+
* <li><code>org.slf4j.simpleLogger.showDateTime</code> - If you would like the
71+
* current date and time to be included in output messages, then set it to
72+
* <code>true</code>. Default is <code>false</code></li>
7373
*
7474
* <li><code>org.slf4j.simpleLogger.dateTimeFormat</code> - The date and time
7575
* format to be used in the output messages. The pattern describing the date and
@@ -78,13 +78,17 @@
7878
* <code>SimpleDateFormat</code></a>. If the format is not specified or is
7979
* invalid, the number of milliseconds since start up will be output.</li>
8080
*
81-
* <li><code>org.slf4j.simpleLogger.showThreadName</code> -Set to
82-
* <code>true</code> if you want to output the current thread name. Defaults to
81+
* <li><code>org.slf4j.simpleLogger.showThreadName</code> - If you want to output
82+
* the current thread name, then set it to <code>true</code>. Defaults to
8383
* <code>true</code>.</li>
8484
*
85-
* <li><code>org.slf4j.simpleLogger.showLogName</code> - Set to
86-
* <code>true</code> if you want the Logger instance name to be included in
87-
* output messages. Defaults to <code>true</code>.</li>
85+
* <li>(since version 1.7.33 and 2.0.0-alpha6) <code>org.slf4j.simpleLogger.showThreadId</code> -
86+
* If you would like to output the current thread name, then set to
87+
* <code>true</code>. Defaults to <code>false</code>.</li>
88+
*
89+
* <li><code>org.slf4j.simpleLogger.showLogName</code> - If you would like
90+
* the Logger instance name to be included in output messages, then set it to
91+
* <code>true</code>. Defaults to <code>true</code>.</li>
8892
*
8993
* <li><code>org.slf4j.simpleLogger.showShortLogName</code> - Set to
9094
* <code>true</code> if you want the last component of the name to be included
@@ -142,7 +146,8 @@
142146
*/
143147
public class SimpleLogger extends MarkerIgnoringBase {
144148

145-
private static final long serialVersionUID = -632788891211436180L;
149+
150+
private static final long serialVersionUID = -632788891211436180L;
146151

147152
private static long START_TIME = System.currentTimeMillis();
148153

@@ -151,6 +156,9 @@ public class SimpleLogger extends MarkerIgnoringBase {
151156
protected static final int LOG_LEVEL_INFO = LocationAwareLogger.INFO_INT;
152157
protected static final int LOG_LEVEL_WARN = LocationAwareLogger.WARN_INT;
153158
protected static final int LOG_LEVEL_ERROR = LocationAwareLogger.ERROR_INT;
159+
160+
private static final String TID_PREFIX = "tid=";
161+
154162
// The OFF level can only be used in configuration files to disable logging.
155163
// It has
156164
// no printing method associated with it in o.s.Logger interface.
@@ -200,6 +208,8 @@ static void init() {
200208

201209
public static final String SHOW_THREAD_NAME_KEY = SimpleLogger.SYSTEM_PREFIX + "showThreadName";
202210

211+
public static final String SHOW_THREAD_ID_KEY = SimpleLogger.SYSTEM_PREFIX + "showThreadId";
212+
203213
public static final String DATE_TIME_FORMAT_KEY = SimpleLogger.SYSTEM_PREFIX + "dateTimeFormat";
204214

205215
public static final String SHOW_DATE_TIME_KEY = SimpleLogger.SYSTEM_PREFIX + "showDateTime";
@@ -269,6 +279,12 @@ private void log(int level, String message, Throwable t) {
269279
buf.append("] ");
270280
}
271281

282+
if (CONFIG_PARAMS.showThreadId) {
283+
buf.append(TID_PREFIX);
284+
buf.append(Thread.currentThread().getId());
285+
buf.append(' ');
286+
}
287+
272288
if (CONFIG_PARAMS.levelInBrackets)
273289
buf.append('[');
274290

@@ -326,7 +342,6 @@ void write(StringBuilder buf, Throwable t) {
326342
writeThrowable(t, targetStream);
327343
targetStream.flush();
328344
}
329-
330345
}
331346

332347
protected void writeThrowable(Throwable t, PrintStream targetStream) {

slf4j-simple/src/main/java/org/slf4j/impl/SimpleLoggerConfiguration.java

+9
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ public class SimpleLoggerConfiguration {
4545
private static final boolean SHOW_THREAD_NAME_DEFAULT = true;
4646
boolean showThreadName = SHOW_THREAD_NAME_DEFAULT;
4747

48+
/**
49+
* See https://jira.qos.ch/browse/SLF4J-499
50+
* @since 1.7.33 and 2.0.0-alpha6
51+
*/
52+
private static final boolean SHOW_THREAD_ID_DEFAULT = false;
53+
boolean showThreadId = SHOW_THREAD_ID_DEFAULT;
54+
4855
final static boolean SHOW_LOG_NAME_DEFAULT = true;
4956
boolean showLogName = SHOW_LOG_NAME_DEFAULT;
5057

@@ -77,6 +84,8 @@ void init() {
7784
showShortLogName = getBooleanProperty(SimpleLogger.SHOW_SHORT_LOG_NAME_KEY, SHOW_SHORT_LOG_NAME_DEFAULT);
7885
showDateTime = getBooleanProperty(SimpleLogger.SHOW_DATE_TIME_KEY, SHOW_DATE_TIME_DEFAULT);
7986
showThreadName = getBooleanProperty(SimpleLogger.SHOW_THREAD_NAME_KEY, SHOW_THREAD_NAME_DEFAULT);
87+
showThreadId = getBooleanProperty(SimpleLogger.SHOW_THREAD_ID_KEY, SHOW_THREAD_ID_DEFAULT);
88+
8089
dateTimeFormatStr = getStringProperty(SimpleLogger.DATE_TIME_FORMAT_KEY, DATE_TIME_FORMAT_STR_DEFAULT);
8190
levelInBrackets = getBooleanProperty(SimpleLogger.LEVEL_IN_BRACKETS_KEY, LEVEL_IN_BRACKETS_DEFAULT);
8291
warnLevelString = getStringProperty(SimpleLogger.WARN_LEVEL_STRING_KEY, WARN_LEVELS_STRING_DEFAULT);

slf4j-simple/src/test/java/org/slf4j/impl/SimpleLoggerTest.java

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) 2004-2012 QOS.ch
2+
* Copyright (c) 2004-2022 QOS.ch Sarl (Switzerland)
33
* All rights reserved.
44
*
55
* Permission is hereby granted, free of charge, to any person obtaining
@@ -31,6 +31,7 @@
3131

3232
import java.io.ByteArrayOutputStream;
3333
import java.io.PrintStream;
34+
import java.util.regex.Pattern;
3435

3536
import org.junit.After;
3637
import org.junit.Before;
@@ -52,6 +53,8 @@ public void before() {
5253
public void after() {
5354
System.clearProperty(A_KEY);
5455
System.clearProperty(SimpleLogger.CACHE_OUTPUT_STREAM_STRING_KEY);
56+
System.clearProperty(SimpleLogger.SHOW_THREAD_ID_KEY);
57+
System.clearProperty(SimpleLogger.SHOW_THREAD_NAME_KEY);
5558
System.setErr(original);
5659
}
5760

@@ -120,4 +123,30 @@ public void checkUseOfCachedOutputStream() {
120123
replacement.flush();
121124
assertTrue(bout.toString().contains("INFO org.slf4j.impl.SimpleLoggerTest - hello"));
122125
}
126+
127+
@Test
128+
public void testTheadIdWithoutThreadName () {
129+
System.setProperty(SimpleLogger.SHOW_THREAD_NAME_KEY, Boolean.FALSE.toString());
130+
String patternStr = "^tid=\\d{1,12} INFO org.slf4j.impl.SimpleLoggerTest - hello";
131+
commonTestThreadId(patternStr);
132+
}
133+
134+
@Test
135+
public void testThreadId() {
136+
String patternStr = "^\\[.*\\] tid=\\d{1,12} INFO org.slf4j.impl.SimpleLoggerTest - hello";
137+
commonTestThreadId(patternStr);
138+
}
139+
private void commonTestThreadId(String patternStr) {
140+
System.setErr(replacement);
141+
System.setProperty(SimpleLogger.SHOW_THREAD_ID_KEY, Boolean.TRUE.toString());
142+
SimpleLogger.init();
143+
SimpleLogger simpleLogger = new SimpleLogger(this.getClass().getName());
144+
simpleLogger.info("hello");
145+
replacement.flush();
146+
String output = bout.toString();
147+
System.out.println(patternStr);
148+
System.out.println(output);
149+
assertTrue(Pattern.compile(patternStr).matcher(output).lookingAt());
150+
}
151+
123152
}

0 commit comments

Comments
 (0)