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.ByteArrayOutputStream;
20  import java.io.PrintStream;
21  import java.text.DateFormat;
22  import java.text.SimpleDateFormat;
23  import java.util.Date;
24  import java.util.Map;
25  
26  import org.apache.logging.log4j.Level;
27  import org.apache.logging.log4j.Marker;
28  import org.apache.logging.log4j.ThreadContext;
29  import org.apache.logging.log4j.message.Message;
30  import org.apache.logging.log4j.message.MessageFactory;
31  import org.apache.logging.log4j.spi.AbstractLogger;
32  import org.apache.logging.log4j.util.PropertiesUtil;
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  
62      public SimpleLogger(final String name, final Level defaultLevel, final boolean showLogName,
63                          final boolean showShortLogName, final boolean showDateTime, final boolean showContextMap,
64                          final String dateTimeFormat, final MessageFactory messageFactory, final PropertiesUtil props,
65                          final PrintStream stream) {
66          super(name, messageFactory);
67          final String lvl = props.getStringProperty(SimpleLoggerContext.SYSTEM_PREFIX + name + ".level");
68          this.level = Level.toLevel(lvl, defaultLevel);
69          if (showShortLogName) {
70              final int index = name.lastIndexOf(".");
71              if (index > 0 && index < name.length()) {
72                  this.logName = name.substring(index + 1);
73              } else {
74                  this.logName = name;
75              }
76          } else if (showLogName) {
77              this.logName = name;
78          } else {
79          	this.logName = null;
80          }
81          this.showDateTime = showDateTime;
82          this.showContextMap = showContextMap;
83          this.stream = stream;
84  
85          if (showDateTime) {
86              try {
87                  this.dateFormatter = new SimpleDateFormat(dateTimeFormat);
88              } catch (final IllegalArgumentException e) {
89                  // If the format pattern is invalid - use the default format
90                  this.dateFormatter = new SimpleDateFormat(SimpleLoggerContext.DEFAULT_DATE_TIME_FORMAT);
91              }
92          }
93      }
94  
95      @Override
96      public Level getLevel() {
97          return level;
98      }
99  
100     @Override
101     public boolean isEnabled(final Level level, final Marker marker, final Message msg, final Throwable t) {
102         return this.level.intLevel() >= level.intLevel();
103     }
104 
105     @Override
106     public boolean isEnabled(final Level level, final Marker marker, final Object msg, final Throwable t) {
107         return this.level.intLevel() >= level.intLevel();
108     }
109 
110     @Override
111     public boolean isEnabled(final Level level, final Marker marker, final String msg) {
112         return this.level.intLevel() >= level.intLevel();
113     }
114 
115     @Override
116     public boolean isEnabled(final Level level, final Marker marker, final String msg, final Object... p1) {
117         return this.level.intLevel() >= level.intLevel();
118     }
119 
120     @Override
121     public boolean isEnabled(final Level level, final Marker marker, final String msg, final Throwable t) {
122         return this.level.intLevel() >= level.intLevel();
123     }
124 
125     @Override
126     public void logMessage(final String fqcn, final Level level, final Marker marker, final Message msg, 
127             final Throwable throwable) {
128         final StringBuilder sb = new StringBuilder();
129         // Append date-time if so configured
130         if (showDateTime) {
131             final Date now = new Date();
132             String dateText;
133             synchronized (dateFormatter) {
134                 dateText = dateFormatter.format(now);
135             }
136             sb.append(dateText);
137             sb.append(SPACE);
138         }
139 
140         sb.append(level.toString());
141         sb.append(SPACE);
142         if (logName != null && logName.length() > 0) {
143             sb.append(logName);
144             sb.append(SPACE);
145         }
146         sb.append(msg.getFormattedMessage());
147         if (showContextMap) {
148             final Map<String, String> mdc = ThreadContext.getContext();
149             if (mdc.size() > 0) {
150                 sb.append(SPACE);
151                 sb.append(mdc.toString());
152                 sb.append(SPACE);
153             }
154         }
155         final Object[] params = msg.getParameters();
156         Throwable t;
157         if (throwable == null && params != null && params.length > 0 && params[params.length - 1] instanceof Throwable) {
158             t = (Throwable) params[params.length - 1];
159         } else {
160             t = throwable;
161         }
162         if (t != null) {
163             sb.append(SPACE);
164             final ByteArrayOutputStream baos = new ByteArrayOutputStream();
165             t.printStackTrace(new PrintStream(baos));
166             sb.append(baos.toString());
167         }
168         stream.println(sb.toString());
169     }
170 
171     public void setLevel(final Level level) {
172         if (level != null) {
173             this.level = level;
174         }
175     }
176 
177     public void setStream(final PrintStream stream) {
178         this.stream = stream;
179     }
180 
181 }