001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.simple;
018
019import java.io.FileNotFoundException;
020import java.io.FileOutputStream;
021import java.io.PrintStream;
022
023import org.apache.logging.log4j.Level;
024import org.apache.logging.log4j.message.MessageFactory;
025import org.apache.logging.log4j.spi.AbstractLogger;
026import org.apache.logging.log4j.spi.ExtendedLogger;
027import org.apache.logging.log4j.spi.LoggerContext;
028import org.apache.logging.log4j.spi.LoggerRegistry;
029import org.apache.logging.log4j.util.PropertiesUtil;
030
031/**
032 * A simple {@link LoggerContext} implementation.
033 */
034public class SimpleLoggerContext implements LoggerContext {
035
036    /** Singleton instance. */
037    static final SimpleLoggerContext INSTANCE = new SimpleLoggerContext();
038    
039    private static final String SYSTEM_OUT = "system.out";
040
041    private static final String SYSTEM_ERR = "system.err";
042
043    /** The default format to use when formatting dates */
044    protected static final String DEFAULT_DATE_TIME_FORMAT = "yyyy/MM/dd HH:mm:ss:SSS zzz";
045
046    /** All system properties used by <code>SimpleLog</code> start with this */
047    protected static final String SYSTEM_PREFIX = "org.apache.logging.log4j.simplelog.";
048
049    private final PropertiesUtil props;
050
051    /** Include the instance name in the log message? */
052    private final boolean showLogName;
053
054    /**
055     * Include the short name (last component) of the logger in the log message. Defaults to true - otherwise we'll be
056     * lost in a flood of messages without knowing who sends them.
057     */
058    private final boolean showShortName;
059
060    /** Include the current time in the log message */
061    private final boolean showDateTime;
062
063    /** Include the ThreadContextMap in the log message */
064    private final boolean showContextMap;
065    
066    /** The date and time format to use in the log message */
067    private final String dateTimeFormat;
068
069    private final Level defaultLevel;
070
071    private final PrintStream stream;
072
073    private final LoggerRegistry<ExtendedLogger> loggerRegistry = new LoggerRegistry<>();
074
075    /**
076     * Constructs a new initialized instance.
077     */
078    public SimpleLoggerContext() {
079        props = new PropertiesUtil("log4j2.simplelog.properties");
080
081        showContextMap = props.getBooleanProperty(SYSTEM_PREFIX + "showContextMap", false);
082        showLogName = props.getBooleanProperty(SYSTEM_PREFIX + "showlogname", false);
083        showShortName = props.getBooleanProperty(SYSTEM_PREFIX + "showShortLogname", true);
084        showDateTime = props.getBooleanProperty(SYSTEM_PREFIX + "showdatetime", false);
085        final String lvl = props.getStringProperty(SYSTEM_PREFIX + "level");
086        defaultLevel = Level.toLevel(lvl, Level.ERROR);
087
088        dateTimeFormat = showDateTime ? props.getStringProperty(SimpleLoggerContext.SYSTEM_PREFIX + "dateTimeFormat",
089                DEFAULT_DATE_TIME_FORMAT) : null;
090
091        final String fileName = props.getStringProperty(SYSTEM_PREFIX + "logFile", SYSTEM_ERR);
092        PrintStream ps;
093        if (SYSTEM_ERR.equalsIgnoreCase(fileName)) {
094            ps = System.err;
095        } else if (SYSTEM_OUT.equalsIgnoreCase(fileName)) {
096            ps = System.out;
097        } else {
098            try {
099                ps = new PrintStream(new FileOutputStream(fileName));
100            } catch (final FileNotFoundException fnfe) {
101                ps = System.err;
102            }
103        }
104        this.stream = ps;
105    }
106
107    @Override
108    public Object getExternalContext() {
109        return null;
110    }
111
112    @Override
113    public ExtendedLogger getLogger(final String name) {
114        return getLogger(name, null);
115    }
116
117    @Override
118    public ExtendedLogger getLogger(final String name, final MessageFactory messageFactory) {
119        // Note: This is the only method where we add entries to the 'loggerRegistry' ivar.
120        final ExtendedLogger extendedLogger = loggerRegistry.getLogger(name, messageFactory);
121        if (extendedLogger != null) {
122            AbstractLogger.checkMessageFactory(extendedLogger, messageFactory);
123            return extendedLogger;
124        }
125        final SimpleLogger simpleLogger = new SimpleLogger(name, defaultLevel, showLogName, showShortName, showDateTime,
126                showContextMap, dateTimeFormat, messageFactory, props, stream);
127        loggerRegistry.putIfAbsent(name, messageFactory, simpleLogger);
128        return loggerRegistry.getLogger(name, messageFactory);
129    }
130
131    /**
132     * Gets the LoggerRegistry.
133     *
134     * @return the LoggerRegistry.
135     * @since 2.17.2
136     */
137    @Override
138    public LoggerRegistry<ExtendedLogger> getLoggerRegistry() {
139        return loggerRegistry;
140    }
141
142    @Override
143    public boolean hasLogger(final String name) {
144        return false;
145    }
146
147    @Override
148    public boolean hasLogger(final String name, final Class<? extends MessageFactory> messageFactoryClass) {
149        return false;
150    }
151
152    @Override
153    public boolean hasLogger(final String name, final MessageFactory messageFactory) {
154        return false;
155    }
156
157}