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     */
017    package org.apache.logging.log4j.simple;
018    
019    import org.apache.logging.log4j.Level;
020    import org.apache.logging.log4j.Logger;
021    import org.apache.logging.log4j.spi.LoggerContext;
022    import org.apache.logging.log4j.util.PropsUtil;
023    
024    import java.io.FileNotFoundException;
025    import java.io.FileOutputStream;
026    import java.io.PrintStream;
027    import java.util.Properties;
028    import java.util.concurrent.ConcurrentHashMap;
029    import java.util.concurrent.ConcurrentMap;
030    
031    /**
032     *
033     */
034    public class SimpleLoggerContext implements LoggerContext {
035    
036        /** The default format to use when formating dates */
037        protected static final String DEFAULT_DATE_TIME_FORMAT = "yyyy/MM/dd HH:mm:ss:SSS zzz";
038    
039        /** All system properties used by <code>SimpleLog</code> start with this */
040        protected static final String SYSTEM_PREFIX = "org.apache.logging.log4j.simplelog.";
041    
042        /** Properties loaded from simplelog.properties */
043        private Properties simpleLogProps = new Properties();
044    
045        private PropsUtil props;
046    
047        /** Include the instance name in the log message? */
048        private final boolean showLogName;
049        /** Include the short name ( last component ) of the logger in the log
050         *  message. Defaults to true - otherwise we'll be lost in a flood of
051         *  messages without knowing who sends them.
052         */
053        private final boolean showShortName;
054        /** Include the current time in the log message */
055        private final boolean showDateTime;
056        /** Include the ThreadContextMap in the log message */
057        private final boolean showContextMap;
058        /** The date and time format to use in the log message */
059        private final String dateTimeFormat;
060    
061        private final Level defaultLevel;
062    
063        private final PrintStream stream;
064    
065        public SimpleLoggerContext() {
066            props = new PropsUtil("log4j2.simplelog.properties");
067    
068            showContextMap = props.getBooleanProperty(SYSTEM_PREFIX + "showContextMap", false);
069            showLogName = props.getBooleanProperty(SYSTEM_PREFIX + "showlogname", false);
070            showShortName = props.getBooleanProperty(SYSTEM_PREFIX + "showShortLogname", true);
071            showDateTime = props.getBooleanProperty(SYSTEM_PREFIX + "showdatetime", false);
072            String lvl = props.getStringProperty(SYSTEM_PREFIX + "level");
073            defaultLevel = Level.toLevel(lvl, Level.ERROR);
074    
075            dateTimeFormat = showDateTime ? props.getStringProperty(SimpleLoggerContext.SYSTEM_PREFIX + "dateTimeFormat",
076                DEFAULT_DATE_TIME_FORMAT) : null;
077    
078            String fileName = props.getStringProperty(SYSTEM_PREFIX + "logFile", "system.err");
079            PrintStream ps;
080            if ("system.err".equalsIgnoreCase(fileName)) {
081                ps = System.err;
082            } else if ("system.out".equalsIgnoreCase(fileName)) {
083                ps = System.out;
084            } else {
085                try {
086                    FileOutputStream os = new FileOutputStream(fileName);
087                    ps = new PrintStream(os);
088                } catch (FileNotFoundException fnfe) {
089                    ps = System.err;
090                }
091            }
092            this.stream = ps;
093        }
094    
095        private ConcurrentMap<String, Logger> loggers = new ConcurrentHashMap<String, Logger>();
096    
097        public Logger getLogger(String name) {
098            if (loggers.containsKey(name)) {
099                return loggers.get(name);
100            }
101    
102            loggers.putIfAbsent(name, new SimpleLogger(name, defaultLevel, showLogName, showShortName, showDateTime,
103                showContextMap, dateTimeFormat, props, stream));
104            return loggers.get(name);
105        }
106    
107        public boolean hasLogger(String name) {
108            return false;
109        }
110    
111        public Object getExternalContext() {
112            return null;
113        }
114    }