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 org.apache.logging.log4j.Level;
20  import org.apache.logging.log4j.Logger;
21  import org.apache.logging.log4j.spi.LoggerContext;
22  import org.apache.logging.log4j.util.PropsUtil;
23  
24  import java.io.FileNotFoundException;
25  import java.io.FileOutputStream;
26  import java.io.PrintStream;
27  import java.util.Properties;
28  import java.util.concurrent.ConcurrentHashMap;
29  import java.util.concurrent.ConcurrentMap;
30  
31  /**
32   *
33   */
34  public class SimpleLoggerContext implements LoggerContext {
35  
36      /** The default format to use when formating dates */
37      protected static final String DEFAULT_DATE_TIME_FORMAT = "yyyy/MM/dd HH:mm:ss:SSS zzz";
38  
39      /** All system properties used by <code>SimpleLog</code> start with this */
40      protected static final String SYSTEM_PREFIX = "org.apache.logging.log4j.simplelog.";
41  
42      /** Properties loaded from simplelog.properties */
43      private Properties simpleLogProps = new Properties();
44  
45      private PropsUtil props;
46  
47      /** Include the instance name in the log message? */
48      private final boolean showLogName;
49      /** Include the short name ( last component ) of the logger in the log
50       *  message. Defaults to true - otherwise we'll be lost in a flood of
51       *  messages without knowing who sends them.
52       */
53      private final boolean showShortName;
54      /** Include the current time in the log message */
55      private final boolean showDateTime;
56      /** Include the ThreadContextMap in the log message */
57      private final boolean showContextMap;
58      /** The date and time format to use in the log message */
59      private final String dateTimeFormat;
60  
61      private final Level defaultLevel;
62  
63      private final PrintStream stream;
64  
65      public SimpleLoggerContext() {
66          props = new PropsUtil("log4j2.simplelog.properties");
67  
68          showContextMap = props.getBooleanProperty(SYSTEM_PREFIX + "showContextMap", false);
69          showLogName = props.getBooleanProperty(SYSTEM_PREFIX + "showlogname", false);
70          showShortName = props.getBooleanProperty(SYSTEM_PREFIX + "showShortLogname", true);
71          showDateTime = props.getBooleanProperty(SYSTEM_PREFIX + "showdatetime", false);
72          String lvl = props.getStringProperty(SYSTEM_PREFIX + "level");
73          defaultLevel = Level.toLevel(lvl, Level.ERROR);
74  
75          dateTimeFormat = showDateTime ? props.getStringProperty(SimpleLoggerContext.SYSTEM_PREFIX + "dateTimeFormat",
76              DEFAULT_DATE_TIME_FORMAT) : null;
77  
78          String fileName = props.getStringProperty(SYSTEM_PREFIX + "logFile", "system.err");
79          PrintStream ps;
80          if ("system.err".equalsIgnoreCase(fileName)) {
81              ps = System.err;
82          } else if ("system.out".equalsIgnoreCase(fileName)) {
83              ps = System.out;
84          } else {
85              try {
86                  FileOutputStream os = new FileOutputStream(fileName);
87                  ps = new PrintStream(os);
88              } catch (FileNotFoundException fnfe) {
89                  ps = System.err;
90              }
91          }
92          this.stream = ps;
93      }
94  
95      private ConcurrentMap<String, Logger> loggers = new ConcurrentHashMap<String, Logger>();
96  
97      public Logger getLogger(String name) {
98          if (loggers.containsKey(name)) {
99              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 }