Source code

    001package org.slf4j.impl;
    002
    003import java.io.FileNotFoundException;
    004import java.io.FileOutputStream;
    005import java.io.InputStream;
    006import java.io.PrintStream;
    007import java.security.AccessController;
    008import java.security.PrivilegedAction;
    009import java.text.DateFormat;
    010import java.text.SimpleDateFormat;
    011import java.util.Properties;
    012
    013import org.slf4j.helpers.Util;
    014import org.slf4j.impl.OutputChoice.OutputChoiceType;
    015
    016/**
    017 * This class holds configuration values for {@link SimpleLogger}. The
    018 * values are computed at runtime. See {@link SimpleLogger} documentation for
    019 * more information.
    020 *
    021 *
    022 * @author Ceki G&uuml;lc&uuml;
    023 * @author Scott Sanders
    024 * @author Rod Waldhoff
    025 * @author Robert Burrell Donkin
    026 * @author C&eacute;drik LIME
    027 *
    028 * @since 1.7.25
    029 */
    030public class SimpleLoggerConfiguration {
    031
    032    private static final String CONFIGURATION_FILE = "simplelogger.properties";
    033
    034    static int DEFAULT_LOG_LEVEL_DEFAULT = SimpleLogger.LOG_LEVEL_INFO;
    035    int defaultLogLevel = DEFAULT_LOG_LEVEL_DEFAULT;
    036
    037    private static final boolean SHOW_DATE_TIME_DEFAULT = false;
    038    boolean showDateTime = SHOW_DATE_TIME_DEFAULT;
    039
    040    private static final String DATE_TIME_FORMAT_STR_DEFAULT = null;
    041    private static String dateTimeFormatStr = DATE_TIME_FORMAT_STR_DEFAULT;
    042
    043    DateFormat dateFormatter = null;
    044
    045    private static final boolean SHOW_THREAD_NAME_DEFAULT = true;
    046    boolean showThreadName = SHOW_THREAD_NAME_DEFAULT;
    047
    048    final static boolean SHOW_LOG_NAME_DEFAULT = true;
    049    boolean showLogName = SHOW_LOG_NAME_DEFAULT;
    050
    051    private static final boolean SHOW_SHORT_LOG_NAME_DEFAULT = false;
    052    boolean showShortLogName = SHOW_SHORT_LOG_NAME_DEFAULT;
    053
    054    private static final boolean LEVEL_IN_BRACKETS_DEFAULT = false;
    055    boolean levelInBrackets = LEVEL_IN_BRACKETS_DEFAULT;
    056
    057    private static String LOG_FILE_DEFAULT = "System.err";
    058    private String logFile = LOG_FILE_DEFAULT;
    059    OutputChoice outputChoice = null;
    060
    061    private static final boolean CACHE_OUTPUT_STREAM_DEFAULT = false;
    062    private boolean cacheOutputStream = CACHE_OUTPUT_STREAM_DEFAULT;
    063
    064    private static final String WARN_LEVELS_STRING_DEFAULT = "WARN";
    065    String warnLevelString = WARN_LEVELS_STRING_DEFAULT;
    066
    067    private final Properties properties = new Properties();
    068
    069    void init() {
    070        loadProperties();
    071
    072        String defaultLogLevelString = getStringProperty(SimpleLogger.DEFAULT_LOG_LEVEL_KEY, null);
    073        if (defaultLogLevelString != null)
    074            defaultLogLevel = stringToLevel(defaultLogLevelString);
    075
    076        showLogName = getBooleanProperty(SimpleLogger.SHOW_LOG_NAME_KEY, SimpleLoggerConfiguration.SHOW_LOG_NAME_DEFAULT);
    077        showShortLogName = getBooleanProperty(SimpleLogger.SHOW_SHORT_LOG_NAME_KEY, SHOW_SHORT_LOG_NAME_DEFAULT);
    078        showDateTime = getBooleanProperty(SimpleLogger.SHOW_DATE_TIME_KEY, SHOW_DATE_TIME_DEFAULT);
    079        showThreadName = getBooleanProperty(SimpleLogger.SHOW_THREAD_NAME_KEY, SHOW_THREAD_NAME_DEFAULT);
    080        dateTimeFormatStr = getStringProperty(SimpleLogger.DATE_TIME_FORMAT_KEY, DATE_TIME_FORMAT_STR_DEFAULT);
    081        levelInBrackets = getBooleanProperty(SimpleLogger.LEVEL_IN_BRACKETS_KEY, LEVEL_IN_BRACKETS_DEFAULT);
    082        warnLevelString = getStringProperty(SimpleLogger.WARN_LEVEL_STRING_KEY, WARN_LEVELS_STRING_DEFAULT);
    083
    084        logFile = getStringProperty(SimpleLogger.LOG_FILE_KEY, logFile);
    085
    086        cacheOutputStream = getBooleanProperty(SimpleLogger.CACHE_OUTPUT_STREAM_STRING_KEY, CACHE_OUTPUT_STREAM_DEFAULT);
    087        outputChoice = computeOutputChoice(logFile, cacheOutputStream);
    088
    089        if (dateTimeFormatStr != null) {
    090            try {
    091                dateFormatter = new SimpleDateFormat(dateTimeFormatStr);
    092            } catch (IllegalArgumentException e) {
    093                Util.report("Bad date format in " + CONFIGURATION_FILE + "; will output relative time", e);
    094            }
    095        }
    096    }
    097
    098    private void loadProperties() {
    099        // Add props from the resource simplelogger.properties
    100        InputStream in = AccessController.doPrivileged(new PrivilegedAction<InputStream>() {
    101            public InputStream run() {
    102                ClassLoader threadCL = Thread.currentThread().getContextClassLoader();
    103                if (threadCL != null) {
    104                    return threadCL.getResourceAsStream(CONFIGURATION_FILE);
    105                } else {
    106                    return ClassLoader.getSystemResourceAsStream(CONFIGURATION_FILE);
    107                }
    108            }
    109        });
    110        if (null != in) {
    111            try {
    112                properties.load(in);
    113            } catch (java.io.IOException e) {
    114                // ignored
    115            } finally {
    116                try {
    117                    in.close();
    118                } catch (java.io.IOException e) {
    119                    // ignored
    120                }
    121            }
    122        }
    123    }
    124
    125    String getStringProperty(String name, String defaultValue) {
    126        String prop = getStringProperty(name);
    127        return (prop == null) ? defaultValue : prop;
    128    }
    129
    130    boolean getBooleanProperty(String name, boolean defaultValue) {
    131        String prop = getStringProperty(name);
    132        return (prop == null) ? defaultValue : "true".equalsIgnoreCase(prop);
    133    }
    134
    135    String getStringProperty(String name) {
    136        String prop = null;
    137        try {
    138            prop = System.getProperty(name);
    139        } catch (SecurityException e) {
    140            ; // Ignore
    141        }
    142        return (prop == null) ? properties.getProperty(name) : prop;
    143    }
    144
    145    static int stringToLevel(String levelStr) {
    146        if ("trace".equalsIgnoreCase(levelStr)) {
    147            return SimpleLogger.LOG_LEVEL_TRACE;
    148        } else if ("debug".equalsIgnoreCase(levelStr)) {
    149            return SimpleLogger.LOG_LEVEL_DEBUG;
    150        } else if ("info".equalsIgnoreCase(levelStr)) {
    151            return SimpleLogger.LOG_LEVEL_INFO;
    152        } else if ("warn".equalsIgnoreCase(levelStr)) {
    153            return SimpleLogger.LOG_LEVEL_WARN;
    154        } else if ("error".equalsIgnoreCase(levelStr)) {
    155            return SimpleLogger.LOG_LEVEL_ERROR;
    156        } else if ("off".equalsIgnoreCase(levelStr)) {
    157            return SimpleLogger.LOG_LEVEL_OFF;
    158        }
    159        // assume INFO by default
    160        return SimpleLogger.LOG_LEVEL_INFO;
    161    }
    162
    163    private static OutputChoice computeOutputChoice(String logFile, boolean cacheOutputStream) {
    164        if ("System.err".equalsIgnoreCase(logFile))
    165            if (cacheOutputStream)
    166                return new OutputChoice(OutputChoiceType.CACHED_SYS_ERR);
    167            else
    168                return new OutputChoice(OutputChoiceType.SYS_ERR);
    169        else if ("System.out".equalsIgnoreCase(logFile)) {
    170            if (cacheOutputStream)
    171                return new OutputChoice(OutputChoiceType.CACHED_SYS_OUT);
    172            else
    173                return new OutputChoice(OutputChoiceType.SYS_OUT);
    174        } else {
    175            try {
    176                FileOutputStream fos = new FileOutputStream(logFile);
    177                PrintStream printStream = new PrintStream(fos);
    178                return new OutputChoice(printStream);
    179            } catch (FileNotFoundException e) {
    180                Util.report("Could not open [" + logFile + "]. Defaulting to System.err", e);
    181                return new OutputChoice(OutputChoiceType.SYS_ERR);
    182            }
    183        }
    184    }
    185
    186}