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      public void setStream(final PrintStream stream) {
96          this.stream = stream;
97      }
98  
99      public Level getLevel() {
100         return level;
101     }
102 
103     public void setLevel(final Level level) {
104         if (level != null) {
105             this.level = level;
106         }
107     }
108 
109     @Override
110     public void log(final Marker marker, final String fqcn, final Level level, final Message msg,
111                     final Throwable throwable) {
112         final StringBuilder sb = new StringBuilder();
113         // Append date-time if so configured
114         if (showDateTime) {
115             final Date now = new Date();
116             String dateText;
117             synchronized (dateFormatter) {
118                 dateText = dateFormatter.format(now);
119             }
120             sb.append(dateText);
121             sb.append(SPACE);
122         }
123 
124         sb.append(level.toString());
125         sb.append(SPACE);
126         if (logName != null && logName.length() > 0) {
127             sb.append(logName);
128             sb.append(SPACE);
129         }
130         sb.append(msg.getFormattedMessage());
131         if (showContextMap) {
132             final Map<String, String> mdc = ThreadContext.getContext();
133             if (mdc.size() > 0) {
134                 sb.append(SPACE);
135                 sb.append(mdc.toString());
136                 sb.append(SPACE);
137             }
138         }
139         final Object[] params = msg.getParameters();
140         Throwable t;
141         if (throwable == null && params != null && params[params.length - 1] instanceof Throwable) {
142             t = (Throwable) params[params.length - 1];
143         } else {
144             t = throwable;
145         }
146         if (t != null) {
147             sb.append(SPACE);
148             final ByteArrayOutputStream baos = new ByteArrayOutputStream();
149             t.printStackTrace(new PrintStream(baos));
150             sb.append(baos.toString());
151         }
152         stream.println(sb.toString());
153     }
154 
155     @Override
156     protected boolean isEnabled(final Level level, final Marker marker, final String msg) {
157         return this.level.intLevel() >= level.intLevel();
158     }
159 
160 
161     @Override
162     protected boolean isEnabled(final Level level, final Marker marker, final String msg, final Throwable t) {
163         return this.level.intLevel() >= level.intLevel();
164     }
165 
166     @Override
167     protected boolean isEnabled(final Level level, final Marker marker, final String msg, final Object... p1) {
168         return this.level.intLevel() >= level.intLevel();
169     }
170 
171     @Override
172     protected boolean isEnabled(final Level level, final Marker marker, final Object msg, final Throwable t) {
173         return this.level.intLevel() >= level.intLevel();
174     }
175 
176     @Override
177     protected boolean isEnabled(final Level level, final Marker marker, final Message msg, final Throwable t) {
178         return this.level.intLevel() >= level.intLevel();
179     }
180 
181 }