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.PrintStream;
20  import java.text.DateFormat;
21  import java.text.SimpleDateFormat;
22  import java.util.Date;
23  import java.util.Map;
24  
25  import org.apache.logging.log4j.Level;
26  import org.apache.logging.log4j.Marker;
27  import org.apache.logging.log4j.ThreadContext;
28  import org.apache.logging.log4j.message.Message;
29  import org.apache.logging.log4j.message.MessageFactory;
30  import org.apache.logging.log4j.spi.AbstractLogger;
31  import org.apache.logging.log4j.util.PropertiesUtil;
32  import org.apache.logging.log4j.util.Strings;
33  
34  /**
35   * This is the default logger that is used when no suitable logging implementation is available.
36   */
37  public class SimpleLogger extends AbstractLogger {
38  
39      private static final long serialVersionUID = 1L;
40  
41      private static final char SPACE = ' ';
42  
43      /**
44       * Used to format times.
45       * <p>
46       * Note that DateFormat is not Thread-safe.
47       * </p>
48       */
49      private DateFormat dateFormatter;
50  
51      private Level level;
52  
53      private final boolean showDateTime;
54  
55      private final boolean showContextMap;
56  
57      private PrintStream stream;
58  
59      private final String logName;
60  
61      public SimpleLogger(final String name, final Level defaultLevel, final boolean showLogName,
62              final boolean showShortLogName, final boolean showDateTime, final boolean showContextMap,
63              final String dateTimeFormat, final MessageFactory messageFactory, final PropertiesUtil props,
64              final PrintStream stream) {
65          super(name, messageFactory);
66          final String lvl = props.getStringProperty(SimpleLoggerContext.SYSTEM_PREFIX + name + ".level");
67          this.level = Level.toLevel(lvl, defaultLevel);
68          if (showShortLogName) {
69              final int index = name.lastIndexOf(".");
70              if (index > 0 && index < name.length()) {
71                  this.logName = name.substring(index + 1);
72              } else {
73                  this.logName = name;
74              }
75          } else if (showLogName) {
76              this.logName = name;
77          } else {
78              this.logName = null;
79          }
80          this.showDateTime = showDateTime;
81          this.showContextMap = showContextMap;
82          this.stream = stream;
83  
84          if (showDateTime) {
85              try {
86                  this.dateFormatter = new SimpleDateFormat(dateTimeFormat);
87              } catch (final IllegalArgumentException e) {
88                  // If the format pattern is invalid - use the default format
89                  this.dateFormatter = new SimpleDateFormat(SimpleLoggerContext.DEFAULT_DATE_TIME_FORMAT);
90              }
91          }
92      }
93  
94      @Override
95      public Level getLevel() {
96          return level;
97      }
98  
99      @Override
100     public boolean isEnabled(final Level testLevel, final Marker marker, final Message msg, final Throwable t) {
101         return this.level.intLevel() >= testLevel.intLevel();
102     }
103 
104     @Override
105     public boolean isEnabled(final Level testLevel, final Marker marker, final Object msg, final Throwable t) {
106         return this.level.intLevel() >= testLevel.intLevel();
107     }
108 
109     @Override
110     public boolean isEnabled(final Level testLevel, final Marker marker, final String msg) {
111         return this.level.intLevel() >= testLevel.intLevel();
112     }
113 
114     @Override
115     public boolean isEnabled(final Level testLevel, final Marker marker, final String msg, final Object... p1) {
116         return this.level.intLevel() >= testLevel.intLevel();
117     }
118 
119     @Override
120     public boolean isEnabled(final Level testLevel, final Marker marker, final String msg, final Throwable t) {
121         return this.level.intLevel() >= testLevel.intLevel();
122     }
123 
124     @Override
125     public void logMessage(final String fqcn, final Level mgsLevel, final Marker marker, final Message msg,
126             final Throwable throwable) {
127         final StringBuilder sb = new StringBuilder();
128         // Append date-time if so configured
129         if (showDateTime) {
130             final Date now = new Date();
131             String dateText;
132             synchronized (dateFormatter) {
133                 dateText = dateFormatter.format(now);
134             }
135             sb.append(dateText);
136             sb.append(SPACE);
137         }
138 
139         sb.append(mgsLevel.toString());
140         sb.append(SPACE);
141         if (Strings.isNotEmpty(logName)) {
142             sb.append(logName);
143             sb.append(SPACE);
144         }
145         sb.append(msg.getFormattedMessage());
146         if (showContextMap) {
147             final Map<String, String> mdc = ThreadContext.getImmutableContext();
148             if (mdc.size() > 0) {
149                 sb.append(SPACE);
150                 sb.append(mdc.toString());
151                 sb.append(SPACE);
152             }
153         }
154         final Object[] params = msg.getParameters();
155         Throwable t;
156         if (throwable == null && params != null && params.length > 0
157                 && params[params.length - 1] instanceof Throwable) {
158             t = (Throwable) params[params.length - 1];
159         } else {
160             t = throwable;
161         }
162         stream.println(sb.toString());
163         if (t != null) {
164             stream.print(SPACE);
165             t.printStackTrace(stream);
166         }
167     }
168 
169     public void setLevel(final Level level) {
170         if (level != null) {
171             this.level = level;
172         }
173     }
174 
175     public void setStream(final PrintStream stream) {
176         this.stream = stream;
177     }
178 
179 }