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