From a0681b72eb0d7c40733f02633e4f2677997b6fce Mon Sep 17 00:00:00 2001 From: Ceki Gulcu Date: Thu, 13 Jan 2022 15:16:39 +0100 Subject: [PATCH] fix SLF4J-499 Signed-off-by: Ceki Gulcu --- .../java/org/slf4j/impl/SimpleLogger.java | 43 +++++++++++++------ .../slf4j/impl/SimpleLoggerConfiguration.java | 9 ++++ .../java/org/slf4j/impl/SimpleLoggerTest.java | 31 ++++++++++++- 3 files changed, 68 insertions(+), 15 deletions(-) diff --git a/slf4j-simple/src/main/java/org/slf4j/impl/SimpleLogger.java b/slf4j-simple/src/main/java/org/slf4j/impl/SimpleLogger.java index 0a3aeeaf4..4ef94a16c 100644 --- a/slf4j-simple/src/main/java/org/slf4j/impl/SimpleLogger.java +++ b/slf4j-simple/src/main/java/org/slf4j/impl/SimpleLogger.java @@ -56,20 +56,20 @@ * * *
  • org.slf4j.simpleLogger.defaultLogLevel - 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".
  • + * for all instances of SimpleLogger. Must be one of "trace", "debug", "info", + * "warn", "error" or "off". If not specified, defaults to "info". * - *
  • org.slf4j.simpleLogger.log.a.b.c - Logging detail - * level for a SimpleLogger instance named "a.b.c". Right-side value must be one + *
  • org.slf4j.simpleLogger.log.com.foo.bar - 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 * org.slf4j.simpleLogger.defaultLogLevel will be used.
  • * - *
  • org.slf4j.simpleLogger.showDateTime - Set to - * true if you want the current date and time to be included in - * output messages. Default is false
  • + *
  • org.slf4j.simpleLogger.showDateTime - If you would like the + * current date and time to be included in output messages, then set it to + * true. Default is false
  • * *
  • org.slf4j.simpleLogger.dateTimeFormat - The date and time * format to be used in the output messages. The pattern describing the date and @@ -78,13 +78,17 @@ * SimpleDateFormat. If the format is not specified or is * invalid, the number of milliseconds since start up will be output.
  • * - *
  • org.slf4j.simpleLogger.showThreadName -Set to - * true if you want to output the current thread name. Defaults to + *
  • org.slf4j.simpleLogger.showThreadName - If you want to output + * the current thread name, then set it to true. Defaults to * true.
  • * - *
  • org.slf4j.simpleLogger.showLogName - Set to - * true if you want the Logger instance name to be included in - * output messages. Defaults to true.
  • + *
  • (since version 1.7.33 and 2.0.0-alpha6) org.slf4j.simpleLogger.showThreadId - + * If you would like to output the current thread name, then set to + * true. Defaults to false.
  • + * + *
  • org.slf4j.simpleLogger.showLogName - If you would like + * the Logger instance name to be included in output messages, then set it to + * true. Defaults to true.
  • * *
  • org.slf4j.simpleLogger.showShortLogName - Set to * true if you want the last component of the name to be included @@ -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(); @@ -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. @@ -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"; @@ -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('['); @@ -326,7 +342,6 @@ void write(StringBuilder buf, Throwable t) { writeThrowable(t, targetStream); targetStream.flush(); } - } protected void writeThrowable(Throwable t, PrintStream targetStream) { diff --git a/slf4j-simple/src/main/java/org/slf4j/impl/SimpleLoggerConfiguration.java b/slf4j-simple/src/main/java/org/slf4j/impl/SimpleLoggerConfiguration.java index 08c0b056c..b70cf055a 100755 --- a/slf4j-simple/src/main/java/org/slf4j/impl/SimpleLoggerConfiguration.java +++ b/slf4j-simple/src/main/java/org/slf4j/impl/SimpleLoggerConfiguration.java @@ -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; @@ -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); diff --git a/slf4j-simple/src/test/java/org/slf4j/impl/SimpleLoggerTest.java b/slf4j-simple/src/test/java/org/slf4j/impl/SimpleLoggerTest.java index 620e3ab3b..ab592eaf9 100644 --- a/slf4j-simple/src/test/java/org/slf4j/impl/SimpleLoggerTest.java +++ b/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 @@ -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; @@ -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); } @@ -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()); + } + }