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