View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.simple;
18  
19  import java.io.FileNotFoundException;
20  import java.io.FileOutputStream;
21  import java.io.PrintStream;
22  import java.util.Properties;
23  import java.util.concurrent.ConcurrentHashMap;
24  import java.util.concurrent.ConcurrentMap;
25  
26  import org.apache.logging.log4j.Level;
27  import org.apache.logging.log4j.message.MessageFactory;
28  import org.apache.logging.log4j.spi.AbstractLogger;
29  import org.apache.logging.log4j.spi.LoggerContext;
30  import org.apache.logging.log4j.spi.ExtendedLogger;
31  import org.apache.logging.log4j.util.PropertiesUtil;
32  
33  /**
34   *
35   */
36  public class SimpleLoggerContext implements LoggerContext {
37  
38      /** The default format to use when formatting dates */
39      protected static final String DEFAULT_DATE_TIME_FORMAT = "yyyy/MM/dd HH:mm:ss:SSS zzz";
40  
41      /** All system properties used by <code>SimpleLog</code> start with this */
42      protected static final String SYSTEM_PREFIX = "org.apache.logging.log4j.simplelog.";
43  
44      /** Properties loaded from simplelog.properties */
45      private final Properties simpleLogProps = new Properties();
46  
47      private final PropertiesUtil props;
48  
49      /** Include the instance name in the log message? */
50      private final boolean showLogName;
51      /**
52       * Include the short name ( last component ) of the logger in the log message. Defaults to true - otherwise we'll be
53       * lost in a flood of messages without knowing who sends them.
54       */
55      private final boolean showShortName;
56      /** Include the current time in the log message */
57      private final boolean showDateTime;
58      /** Include the ThreadContextMap in the log message */
59      private final boolean showContextMap;
60      /** The date and time format to use in the log message */
61      private final String dateTimeFormat;
62  
63      private final Level defaultLevel;
64  
65      private final PrintStream stream;
66  
67      private final ConcurrentMap<String, ExtendedLogger> loggers = new ConcurrentHashMap<String, ExtendedLogger>();
68  
69      public SimpleLoggerContext() {
70          props = new PropertiesUtil("log4j2.simplelog.properties");
71  
72          showContextMap = props.getBooleanProperty(SYSTEM_PREFIX + "showContextMap", false);
73          showLogName = props.getBooleanProperty(SYSTEM_PREFIX + "showlogname", false);
74          showShortName = props.getBooleanProperty(SYSTEM_PREFIX + "showShortLogname", true);
75          showDateTime = props.getBooleanProperty(SYSTEM_PREFIX + "showdatetime", false);
76          final String lvl = props.getStringProperty(SYSTEM_PREFIX + "level");
77          defaultLevel = Level.toLevel(lvl, Level.ERROR);
78  
79          dateTimeFormat = showDateTime ? props.getStringProperty(SimpleLoggerContext.SYSTEM_PREFIX + "dateTimeFormat",
80                  DEFAULT_DATE_TIME_FORMAT) : null;
81  
82          final String fileName = props.getStringProperty(SYSTEM_PREFIX + "logFile", "system.err");
83          PrintStream ps;
84          if ("system.err".equalsIgnoreCase(fileName)) {
85              ps = System.err;
86          } else if ("system.out".equalsIgnoreCase(fileName)) {
87              ps = System.out;
88          } else {
89              try {
90                  final FileOutputStream os = new FileOutputStream(fileName);
91                  ps = new PrintStream(os);
92              } catch (final FileNotFoundException fnfe) {
93                  ps = System.err;
94              }
95          }
96          this.stream = ps;
97      }
98  
99      @Override
100     public ExtendedLogger getLogger(final String name) {
101         return getLogger(name, null);
102     }
103 
104     @Override
105     public ExtendedLogger getLogger(final String name, final MessageFactory messageFactory) {
106         if (loggers.containsKey(name)) {
107             final ExtendedLogger logger = loggers.get(name);
108             AbstractLogger.checkMessageFactory(logger, messageFactory);
109             return logger;
110         }
111 
112         loggers.putIfAbsent(name, new SimpleLogger(name, defaultLevel, showLogName, showShortName, showDateTime,
113                 showContextMap, dateTimeFormat, messageFactory, props, stream));
114         return loggers.get(name);
115     }
116 
117     @Override
118     public boolean hasLogger(final String name) {
119         return false;
120     }
121 
122     @Override
123     public Object getExternalContext() {
124         return null;
125     }
126 }