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