Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds configuration to enable/disable show log level #380

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 33 additions & 25 deletions slf4j-simple/src/main/java/org/slf4j/simple/SimpleLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@
* Simple implementation of {@link Logger} that sends all enabled log messages,
* for all defined loggers, to the console ({@code System.err}). The following
* system properties are supported to configure the behavior of this logger:
*
*
*
* <ul>
* <li><code>org.slf4j.simpleLogger.logFile</code> - The output target which can
* be the <em>path</em> to a file, or the special values "System.out" and
* "System.err". Default is "System.err".</li>
*
*
* <li><code>org.slf4j.simpleLogger.cacheOutputStream</code> - If the output
* target is set to "System.out" or "System.err" (see preceding entry), by
* default, logs will be output to the latest value referenced by
* <code>System.out/err</code> variables. By setting this parameter to true, the
* output stream will be cached, i.e. assigned once at initialization time and
* re-used independently of the current value referenced by
* <code>System.out/err</code>.</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>
Expand All @@ -84,11 +84,11 @@
* <li><code>org.slf4j.simpleLogger.showThreadName</code> -Set to
* <code>true</code> if you want to output the current thread name. Defaults to
* <code>true</code>.</li>
*
* <li>(since version 1.7.33 and 2.0.0-alpha6) <code>org.slf4j.simpleLogger.showThreadId</code> -
*
* <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 id, then set to
* <code>true</code>. Defaults to <code>false</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>
Expand All @@ -97,31 +97,35 @@
* <code>true</code> if you want the last component of the name to be included
* in output messages. Defaults to <code>false</code>.</li>
*
* <li>(since version 2.0.10)<code>org.slf4j.simpleLogger.showLogLevel</code> - Set to
* <code>true</code> if you want the Logger level to be included in
* output messages. Defaults to <code>true</code>.</li>
*
* <li><code>org.slf4j.simpleLogger.levelInBrackets</code> - Should the level
* string be output in brackets? Defaults to <code>false</code>.</li>
*
* <li><code>org.slf4j.simpleLogger.warnLevelString</code> - The string value
* output for the warn level. Defaults to <code>WARN</code>.</li>
*
*
* </ul>
*
* <p>
* In addition to looking for system properties with the names specified above,
* this implementation also checks for a class loader resource named
* <code>"simplelogger.properties"</code>, and includes any matching definitions
* from this resource (if it exists).
*
*
*
* <p>
* With no configuration, the default output includes the relative time in
* milliseconds, thread name, the level, logger name, and the message followed
* by the line separator for the host. In log4j terms it amounts to the "%r [%t]
* %level %logger - %m%n" pattern.
*
*
* <p>
* Sample output follows.
*
*
*
*
* <pre>
* 176 [main] INFO examples.Sort - Populating an array of 2 elements in reverse order.
* 225 [main] INFO examples.SortAlgo - Entered the sort method.
Expand All @@ -139,7 +143,7 @@
* This implementation is heavily inspired by
* <a href="http://commons.apache.org/logging/">Apache Commons Logging</a>'s
* SimpleLog.
*
*
*
* @author Ceki G&uuml;lc&uuml;
* @author Scott Sanders
Expand Down Expand Up @@ -170,7 +174,7 @@ public class SimpleLogger extends LegacyAbstractLogger {

private static boolean INITIALIZED = false;
static final SimpleLoggerConfiguration CONFIG_PARAMS = new SimpleLoggerConfiguration();

static void lazyInit() {
if (INITIALIZED) {
return;
Expand Down Expand Up @@ -210,10 +214,12 @@ static void init() {

public static final String SHOW_LOG_NAME_KEY = SimpleLogger.SYSTEM_PREFIX + "showLogName";

public static final String SHOW_LOG_LEVEL_KEY = SimpleLogger.SYSTEM_PREFIX + "showLogLevel";

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 @@ -250,7 +256,7 @@ String recursivelyComputeLevelString() {
/**
* To avoid intermingling of log messages and associated stack traces, the two
* operations are done in a synchronized block.
*
*
* @param buf
* @param t
*/
Expand All @@ -261,7 +267,7 @@ void write(StringBuilder buf, Throwable t) {
targetStream.println(buf.toString());
writeThrowable(t, targetStream);
targetStream.flush();
}
}

}

Expand Down Expand Up @@ -398,22 +404,24 @@ private void innerHandleNormalizedLoggingCall(Level level, List<Marker> markers,
buf.append(Thread.currentThread().getName());
buf.append("] ");
}

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

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

// Append a readable representation of the log level
String levelStr = level.name();
buf.append(levelStr);
if (CONFIG_PARAMS.levelInBrackets)
buf.append(']');
buf.append(SP);
// Append a readable representation of the log level
String levelStr = level.name();
buf.append(levelStr);
if (CONFIG_PARAMS.levelInBrackets)
buf.append(']');
buf.append(SP);
}

// Append the name of the log instance if so configured
if (CONFIG_PARAMS.showShortLogName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
* This class holds configuration values for {@link SimpleLogger}. The
* values are computed at runtime. See {@link SimpleLogger} documentation for
* more information.
*
*
*
*
* @author Ceki G&uuml;lc&uuml;
* @author Scott Sanders
* @author Rod Waldhoff
* @author Robert Burrell Donkin
* @author C&eacute;drik LIME
*
*
* @since 1.7.25
*/
public class SimpleLoggerConfiguration {
Expand All @@ -52,13 +52,16 @@ public class SimpleLoggerConfiguration {
*/
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;

private static final boolean SHOW_SHORT_LOG_NAME_DEFAULT = false;
boolean showShortLogName = SHOW_SHORT_LOG_NAME_DEFAULT;

final static boolean SHOW_LOG_LEVEL_DEFAULT = true;
boolean showLogLevel = SHOW_LOG_LEVEL_DEFAULT;

private static final boolean LEVEL_IN_BRACKETS_DEFAULT = false;
boolean levelInBrackets = LEVEL_IN_BRACKETS_DEFAULT;

Expand Down Expand Up @@ -87,6 +90,7 @@ void init() {
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);
showLogLevel = getBooleanProperty(SimpleLogger.SHOW_LOG_LEVEL_KEY, SimpleLoggerConfiguration.SHOW_LOG_LEVEL_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