Skip to content

Commit

Permalink
fix SLF4J-499
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Jan 13, 2022
1 parent c89dc3d commit a0681b7
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 15 deletions.
43 changes: 29 additions & 14 deletions slf4j-simple/src/main/java/org/slf4j/impl/SimpleLogger.java
Expand Up @@ -56,20 +56,20 @@
* </li>
*
* <li><code>org.slf4j.simpleLogger.defaultLogLevel</code> - Default log level
* for all instances of SimpleLogger. Must be one of ("trace", "debug", "info",
* "warn", "error" or "off"). If not specified, defaults to "info".</li>
* for all instances of SimpleLogger. Must be one of "trace", "debug", "info",
* "warn", "error" or "off". If not specified, defaults to "info".</li>
*
* <li><code>org.slf4j.simpleLogger.log.<em>a.b.c</em></code> - Logging detail
* level for a SimpleLogger instance named "a.b.c". Right-side value must be one
* <li><code>org.slf4j.simpleLogger.log.<em>com.foo.bar</em></code> - Logging detail
* level for a SimpleLogger instance named "com.foo.bar". Right-side value must be one
* of "trace", "debug", "info", "warn", "error" or "off". When a SimpleLogger
* named "a.b.c" is initialized, its level is assigned from this property. If
* unspecified, the level of nearest parent logger will be used, and if none is
* set, then the value specified by
* <code>org.slf4j.simpleLogger.defaultLogLevel</code> will be used.</li>
*
* <li><code>org.slf4j.simpleLogger.showDateTime</code> - Set to
* <code>true</code> if you want the current date and time to be included in
* output messages. Default is <code>false</code></li>
* <li><code>org.slf4j.simpleLogger.showDateTime</code> - If you would like the
* current date and time to be included in output messages, then set it to
* <code>true</code>. Default is <code>false</code></li>
*
* <li><code>org.slf4j.simpleLogger.dateTimeFormat</code> - The date and time
* format to be used in the output messages. The pattern describing the date and
Expand All @@ -78,13 +78,17 @@
* <code>SimpleDateFormat</code></a>. If the format is not specified or is
* invalid, the number of milliseconds since start up will be output.</li>
*
* <li><code>org.slf4j.simpleLogger.showThreadName</code> -Set to
* <code>true</code> if you want to output the current thread name. Defaults to
* <li><code>org.slf4j.simpleLogger.showThreadName</code> - If you want to output
* the current thread name, then set it to <code>true</code>. Defaults to
* <code>true</code>.</li>
*
* <li><code>org.slf4j.simpleLogger.showLogName</code> - Set to
* <code>true</code> if you want the Logger instance name to be included in
* output messages. Defaults to <code>true</code>.</li>
* <li>(since version 1.7.33 and 2.0.0-alpha6) <code>org.slf4j.simpleLogger.showThreadId</code> -
* If you would like to output the current thread name, then set to
* <code>true</code>. Defaults to <code>false</code>.</li>
*
* <li><code>org.slf4j.simpleLogger.showLogName</code> - If you would like
* the Logger instance name to be included in output messages, then set it to
* <code>true</code>. Defaults to <code>true</code>.</li>
*
* <li><code>org.slf4j.simpleLogger.showShortLogName</code> - Set to
* <code>true</code> if you want the last component of the name to be included
Expand Down Expand Up @@ -142,7 +146,8 @@
*/
public class SimpleLogger extends MarkerIgnoringBase {

private static final long serialVersionUID = -632788891211436180L;

private static final long serialVersionUID = -632788891211436180L;

private static long START_TIME = System.currentTimeMillis();

Expand All @@ -151,6 +156,9 @@ public class SimpleLogger extends MarkerIgnoringBase {
protected static final int LOG_LEVEL_INFO = LocationAwareLogger.INFO_INT;
protected static final int LOG_LEVEL_WARN = LocationAwareLogger.WARN_INT;
protected static final int LOG_LEVEL_ERROR = LocationAwareLogger.ERROR_INT;

private static final String TID_PREFIX = "tid=";

// The OFF level can only be used in configuration files to disable logging.
// It has
// no printing method associated with it in o.s.Logger interface.
Expand Down Expand Up @@ -200,6 +208,8 @@ static void init() {

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

public static final String SHOW_THREAD_ID_KEY = SimpleLogger.SYSTEM_PREFIX + "showThreadId";

public static final String DATE_TIME_FORMAT_KEY = SimpleLogger.SYSTEM_PREFIX + "dateTimeFormat";

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

if (CONFIG_PARAMS.showThreadId) {
buf.append(TID_PREFIX);
buf.append(Thread.currentThread().getId());
buf.append(' ');
}

if (CONFIG_PARAMS.levelInBrackets)
buf.append('[');

Expand Down Expand Up @@ -326,7 +342,6 @@ void write(StringBuilder buf, Throwable t) {
writeThrowable(t, targetStream);
targetStream.flush();
}

}

protected void writeThrowable(Throwable t, PrintStream targetStream) {
Expand Down
Expand Up @@ -45,6 +45,13 @@ public class SimpleLoggerConfiguration {
private static final boolean SHOW_THREAD_NAME_DEFAULT = true;
boolean showThreadName = SHOW_THREAD_NAME_DEFAULT;

/**
* See https://jira.qos.ch/browse/SLF4J-499
* @since 1.7.33 and 2.0.0-alpha6
*/
private static final boolean SHOW_THREAD_ID_DEFAULT = false;
boolean showThreadId = SHOW_THREAD_ID_DEFAULT;

final static boolean SHOW_LOG_NAME_DEFAULT = true;
boolean showLogName = SHOW_LOG_NAME_DEFAULT;

Expand Down Expand Up @@ -77,6 +84,8 @@ void init() {
showShortLogName = getBooleanProperty(SimpleLogger.SHOW_SHORT_LOG_NAME_KEY, SHOW_SHORT_LOG_NAME_DEFAULT);
showDateTime = getBooleanProperty(SimpleLogger.SHOW_DATE_TIME_KEY, SHOW_DATE_TIME_DEFAULT);
showThreadName = getBooleanProperty(SimpleLogger.SHOW_THREAD_NAME_KEY, SHOW_THREAD_NAME_DEFAULT);
showThreadId = getBooleanProperty(SimpleLogger.SHOW_THREAD_ID_KEY, SHOW_THREAD_ID_DEFAULT);

dateTimeFormatStr = getStringProperty(SimpleLogger.DATE_TIME_FORMAT_KEY, DATE_TIME_FORMAT_STR_DEFAULT);
levelInBrackets = getBooleanProperty(SimpleLogger.LEVEL_IN_BRACKETS_KEY, LEVEL_IN_BRACKETS_DEFAULT);
warnLevelString = getStringProperty(SimpleLogger.WARN_LEVEL_STRING_KEY, WARN_LEVELS_STRING_DEFAULT);
Expand Down
31 changes: 30 additions & 1 deletion slf4j-simple/src/test/java/org/slf4j/impl/SimpleLoggerTest.java
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2004-2012 QOS.ch
* Copyright (c) 2004-2022 QOS.ch Sarl (Switzerland)
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
Expand Down Expand Up @@ -31,6 +31,7 @@

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.regex.Pattern;

import org.junit.After;
import org.junit.Before;
Expand All @@ -52,6 +53,8 @@ public void before() {
public void after() {
System.clearProperty(A_KEY);
System.clearProperty(SimpleLogger.CACHE_OUTPUT_STREAM_STRING_KEY);
System.clearProperty(SimpleLogger.SHOW_THREAD_ID_KEY);
System.clearProperty(SimpleLogger.SHOW_THREAD_NAME_KEY);
System.setErr(original);
}

Expand Down Expand Up @@ -120,4 +123,30 @@ public void checkUseOfCachedOutputStream() {
replacement.flush();
assertTrue(bout.toString().contains("INFO org.slf4j.impl.SimpleLoggerTest - hello"));
}

@Test
public void testTheadIdWithoutThreadName () {
System.setProperty(SimpleLogger.SHOW_THREAD_NAME_KEY, Boolean.FALSE.toString());
String patternStr = "^tid=\\d{1,12} INFO org.slf4j.impl.SimpleLoggerTest - hello";
commonTestThreadId(patternStr);
}

@Test
public void testThreadId() {
String patternStr = "^\\[.*\\] tid=\\d{1,12} INFO org.slf4j.impl.SimpleLoggerTest - hello";
commonTestThreadId(patternStr);
}
private void commonTestThreadId(String patternStr) {
System.setErr(replacement);
System.setProperty(SimpleLogger.SHOW_THREAD_ID_KEY, Boolean.TRUE.toString());
SimpleLogger.init();
SimpleLogger simpleLogger = new SimpleLogger(this.getClass().getName());
simpleLogger.info("hello");
replacement.flush();
String output = bout.toString();
System.out.println(patternStr);
System.out.println(output);
assertTrue(Pattern.compile(patternStr).matcher(output).lookingAt());
}

}

0 comments on commit a0681b7

Please sign in to comment.