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