org.slf4j.impl.SimpleLogger Maven / Gradle / Ivy
Show all versions of hello-world-plugin Show documentation
/*
* Copyright 2010-2013 Ning, Inc.
* Copyright 2014 Groupon, Inc
* Copyright 2014 The Billing Project, LLC
*
* The Billing Project licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package org.slf4j.impl;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.helpers.FormattingTuple;
import org.slf4j.helpers.MarkerIgnoringBase;
import org.slf4j.helpers.MessageFormatter;
import org.slf4j.helpers.Util;
import org.slf4j.spi.LocationAwareLogger;
/**
* 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:
*
*
* org.slf4j.simpleLogger.logFile
- The output target which can be the path to a file, or
* the special values "System.out" and "System.err". Default is "System.err".
*
* org.slf4j.simpleLogger.defaultLogLevel
- Default log level for all instances of SimpleLogger.
* Must be one of ("trace", "debug", "info", "warn", or "error"). 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 of "trace", "debug", "info", "warn", or "error". 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 true
*
* org.slf4j.simpleLogger.dateTimeFormat
- The date and time format to be used in the output messages.
* The pattern describing the date and time format is defined by
* 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 true
.
*
* org.slf4j.simpleLogger.showLogName
- Set to true
if you want the Logger instance name
* to be included in output messages. Defaults to true
.
*
* org.slf4j.simpleLogger.showShortLogName
- Set to true
if you want the last component
* of the name to be included in output messages. Defaults to false
.
*
* org.slf4j.simpleLogger.levelInBrackets
- Should the level string be output in brackets? Defaults
* to false
.
*
* org.slf4j.simpleLogger.warnLevelString
- The string value output for the warn level. Defaults
* to WARN
.
*
*
*
* In addition to looking for system properties with the names specified above, this implementation also checks for
* a class loader resource named "simplelogger.properties"
, and includes any matching definitions
* from this resource (if it exists).
*
* 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.
* Sample output follows.
*
* 176 [main] INFO examples.Sort - Populating an array of 2 elements in reverse order.
* 225 [main] INFO examples.SortAlgo - Entered the sort method.
* 304 [main] INFO examples.SortAlgo - Dump of integer array:
* 317 [main] INFO examples.SortAlgo - Element [0] = 0
* 331 [main] INFO examples.SortAlgo - Element [1] = 1
* 343 [main] INFO examples.Sort - The next log statement should be an error message.
* 346 [main] ERROR examples.SortAlgo - Tried to dump an uninitialized array.
* at org.log4j.examples.SortAlgo.dump(SortAlgo.java:58)
* at org.log4j.examples.Sort.main(Sort.java:64)
* 467 [main] INFO examples.Sort - Exiting main method.
*
*
* This implementation is heavily inspired by
* Apache Commons Logging's SimpleLog.
*
* @author Ceki Gülcü
* @author Scott Sanders
* @author Rod Waldhoff
* @author Robert Burrell Donkin
* @author Cédrik LIME
*/
public class SimpleLogger extends MarkerIgnoringBase {
private static final long serialVersionUID = -632788891211436180L;
private static final String CONFIGURATION_FILE = "simplelogger.properties";
private static final long START_TIME = System.currentTimeMillis();
private static final Properties SIMPLE_LOGGER_PROPS = new Properties();
private static final int LOG_LEVEL_TRACE = LocationAwareLogger.TRACE_INT;
private static final int LOG_LEVEL_DEBUG = LocationAwareLogger.DEBUG_INT;
private static final int LOG_LEVEL_INFO = LocationAwareLogger.INFO_INT;
private static final int LOG_LEVEL_WARN = LocationAwareLogger.WARN_INT;
private static final int LOG_LEVEL_ERROR = LocationAwareLogger.ERROR_INT;
private static boolean INITIALIZED = false;
private static int DEFAULT_LOG_LEVEL = LOG_LEVEL_INFO;
private static boolean SHOW_DATE_TIME = false;
private static String DATE_TIME_FORMAT_STR = null;
private static DateFormat DATE_FORMATTER = null;
private static boolean SHOW_THREAD_NAME = true;
private static boolean SHOW_LOG_NAME = true;
private static boolean SHOW_SHORT_LOG_NAME = false;
private static String LOG_FILE = "System.err";
private static PrintStream TARGET_STREAM = null;
private static boolean LEVEL_IN_BRACKETS = false;
private static String WARN_LEVEL_STRING = "WARN";
/**
* All system properties used by SimpleLogger
start with this prefix
*/
public static final String SYSTEM_PREFIX = "org.slf4j.simpleLogger.";
public static final String DEFAULT_LOG_LEVEL_KEY = SYSTEM_PREFIX + "defaultLogLevel";
public static final String SHOW_DATE_TIME_KEY = SYSTEM_PREFIX + "showDateTime";
public static final String DATE_TIME_FORMAT_KEY = SYSTEM_PREFIX + "dateTimeFormat";
public static final String SHOW_THREAD_NAME_KEY = SYSTEM_PREFIX + "showThreadName";
public static final String SHOW_LOG_NAME_KEY = SYSTEM_PREFIX + "showLogName";
public static final String SHOW_SHORT_LOG_NAME_KEY = SYSTEM_PREFIX + "showShortLogName";
public static final String LOG_FILE_KEY = SYSTEM_PREFIX + "logFile";
public static final String LEVEL_IN_BRACKETS_KEY = SYSTEM_PREFIX + "levelInBrackets";
public static final String WARN_LEVEL_STRING_KEY = SYSTEM_PREFIX + "warnLevelString";
public static final String LOG_KEY_PREFIX = SYSTEM_PREFIX + "log.";
private static String getStringProperty(final String name) {
String prop = null;
try {
prop = System.getProperty(name);
} catch (final SecurityException e) {
// Ignore
}
return (prop == null) ? SIMPLE_LOGGER_PROPS.getProperty(name) : prop;
}
private static String getStringProperty(final String name, final String defaultValue) {
final String prop = getStringProperty(name);
return (prop == null) ? defaultValue : prop;
}
private static boolean getBooleanProperty(final String name, final boolean defaultValue) {
final String prop = getStringProperty(name);
return (prop == null) ? defaultValue : "true".equalsIgnoreCase(prop);
}
// Initialize class attributes.
// Load properties file, if found.
// Override with system properties.
static void init() {
INITIALIZED = true;
loadProperties();
final String defaultLogLevelString = getStringProperty(DEFAULT_LOG_LEVEL_KEY, null);
if (defaultLogLevelString != null) {
DEFAULT_LOG_LEVEL = stringToLevel(defaultLogLevelString);
}
SHOW_LOG_NAME = getBooleanProperty(SHOW_LOG_NAME_KEY, SHOW_LOG_NAME);
SHOW_SHORT_LOG_NAME = getBooleanProperty(SHOW_SHORT_LOG_NAME_KEY, SHOW_SHORT_LOG_NAME);
SHOW_DATE_TIME = getBooleanProperty(SHOW_DATE_TIME_KEY, SHOW_DATE_TIME);
SHOW_THREAD_NAME = getBooleanProperty(SHOW_THREAD_NAME_KEY, SHOW_THREAD_NAME);
DATE_TIME_FORMAT_STR = getStringProperty(DATE_TIME_FORMAT_KEY, DATE_TIME_FORMAT_STR);
LEVEL_IN_BRACKETS = getBooleanProperty(LEVEL_IN_BRACKETS_KEY, LEVEL_IN_BRACKETS);
WARN_LEVEL_STRING = getStringProperty(WARN_LEVEL_STRING_KEY, WARN_LEVEL_STRING);
LOG_FILE = getStringProperty(LOG_FILE_KEY, LOG_FILE);
TARGET_STREAM = computeTargetStream(LOG_FILE);
if (DATE_TIME_FORMAT_STR != null) {
try {
DATE_FORMATTER = new SimpleDateFormat(DATE_TIME_FORMAT_STR);
} catch (final IllegalArgumentException e) {
Util.report("Bad date format in " + CONFIGURATION_FILE + "; will output relative time", e);
}
}
}
private static PrintStream computeTargetStream(final String logFile) {
if ("System.err".equalsIgnoreCase(logFile)) {
return System.err;
} else if ("System.out".equalsIgnoreCase(logFile)) {
return System.out;
} else {
try {
final FileOutputStream fos = new FileOutputStream(logFile);
final PrintStream printStream = new PrintStream(fos);
return printStream;
} catch (final FileNotFoundException e) {
Util.report("Could not open [" + logFile + "]. Defaulting to System.err", e);
return System.err;
}
}
}
private static void loadProperties() {
// Add props from the resource simplelogger.properties
final InputStream in = (InputStream) AccessController.doPrivileged(
new PrivilegedAction