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.log4j.spi;
18  
19  import org.apache.log4j.ULogger;
20  import org.apache.log4j.helpers.MessageFormatter;
21  
22  
23  /**
24   * A simple implementation that logs messages of level INFO or higher on
25   * the console (<code>System.out</code>).
26   * <p>
27   * The output includes the relative time in milliseconds, thread name, level,
28   * logger name, and the message followed by the line separator for the host.
29   * In log4j terms it amounts to the "%r  [%t] %level %logger - %m%n" pattern.
30   * <pre>
31  176 [main] INFO examples.Sort - Populating an array of 2 elements in reverse.
32  225 [main] INFO examples.SortAlgo - Entered the sort method.
33  304 [main] INFO SortAlgo.DUMP - Dump of interger array:
34  317 [main] INFO SortAlgo.DUMP - Element [0] = 0
35  331 [main] INFO SortAlgo.DUMP - Element [1] = 1
36  343 [main] INFO examples.Sort - The next log statement should be an error msg.
37  346 [main] ERROR SortAlgo.DUMP - Tried to dump an uninitialized array.
38          at org.log4j.examples.SortAlgo.dump(SortAlgo.java:58)
39          at org.log4j.examples.Sort.main(Sort.java:64)
40  467 [main] INFO  examples.Sort - Exiting main method.
41  </pre>
42   *
43   * @author Ceki G&uuml;lc&uuml;
44   */
45  public final class SimpleULogger implements ULogger {
46  
47      /**
48       * Logger name.
49       */
50    private final String loggerName;
51  
52  
53    /**
54     * Mark the time when this class gets loaded into memory.
55     */
56    private static long startTime = System.currentTimeMillis();
57  
58      /**
59       * Line separator.
60       */
61    public static final String LINE_SEPARATOR
62              = System.getProperty("line.separator");
63  
64      /**
65       * INFO string literal.
66       */
67    private static final String INFO_STR = "INFO";
68      /**
69       * WARN string literal.
70       */
71    private static final String WARN_STR = "WARN";
72      /**
73       * ERROR string literal.
74       */
75    private static final String ERROR_STR = "ERROR";
76  
77    /**
78     * Constructor is private to force construction through getLogger.
79     * @param name logger name
80     */
81    private SimpleULogger(final String name) {
82      super();
83      this.loggerName = name;
84    }
85  
86    /**
87     * Creates a new instance.
88     *
89     * @param name logger name
90     * @return  logger.
91     */
92    public static SimpleULogger getLogger(final String name) {
93        return new SimpleULogger(name);
94    }
95  
96    /**
97     * {@inheritDoc}
98     */
99    public boolean isDebugEnabled() {
100     return false;
101   }
102 
103     /**
104      * {@inheritDoc}
105      */
106   public void debug(final Object msg) {
107     // NOP
108   }
109 
110     /**
111      * {@inheritDoc}
112      */
113   public void debug(final Object parameterizedMsg, final Object param1) {
114     // NOP
115   }
116 
117     /**
118      * {@inheritDoc}
119      */
120   public void debug(final String parameterizedMsg,
121                     final Object param1,
122                     final Object param2) {
123     // NOP
124   }
125 
126     /**
127      * {@inheritDoc}
128      */
129   public void debug(final Object msg, final Throwable t) {
130     // NOP
131   }
132 
133   /**
134    * This is our internal implementation for logging regular (non-parameterized)
135    * log messages.
136    *
137    * @param level level
138    * @param message message
139    * @param t throwable
140    */
141   private void log(final String level,
142                    final String message,
143                    final Throwable t) {
144     StringBuffer buf = new StringBuffer();
145 
146     long millis  = System.currentTimeMillis();
147     buf.append(millis - startTime);
148 
149     buf.append(" [");
150     buf.append(Thread.currentThread().getName());
151     buf.append("] ");
152 
153     buf.append(level);
154     buf.append(" ");
155 
156     buf.append(loggerName);
157     buf.append(" - ");
158 
159     buf.append(message);
160 
161     buf.append(LINE_SEPARATOR);
162 
163     System.out.print(buf.toString());
164     if (t != null) {
165       t.printStackTrace(System.out);
166     }
167     System.out.flush();
168   }
169   /**
170    * For parameterized messages, first substitute parameters and then log.
171    *
172    * @param level level
173    * @param parameterizedMsg message pattern
174    * @param param1 param1
175    * @param param2 param2
176    */
177   private void parameterizedLog(final String level,
178                                 final Object parameterizedMsg,
179                                 final Object param1,
180                                 final Object param2) {
181     if (parameterizedMsg instanceof String) {
182       String msgStr = (String) parameterizedMsg;
183       msgStr = MessageFormatter.format(msgStr, param1, param2);
184       log(level, msgStr, null);
185     } else {
186       // To be failsafe, we handle the case where 'messagePattern' is not
187       // a String. Unless the user makes a mistake, this should not happen.
188       log(level, parameterizedMsg.toString(), null);
189     }
190   }
191 
192     /**
193      * {@inheritDoc}
194      */
195   public boolean isInfoEnabled() {
196     return true;
197   }
198 
199     /**
200      * {@inheritDoc}
201      */
202   public void info(final Object msg) {
203     log(INFO_STR, msg.toString(), null);
204   }
205 
206 
207     /**
208      * {@inheritDoc}
209      */
210   public void info(final Object parameterizedMsg, final Object param1) {
211     parameterizedLog(INFO_STR, parameterizedMsg, param1, null);
212   }
213 
214     /**
215      * {@inheritDoc}
216      */
217   public void info(final String parameterizedMsg,
218                    final Object param1,
219                    final Object param2) {
220     parameterizedLog(INFO_STR, parameterizedMsg, param1, param2);
221   }
222 
223     /**
224      * {@inheritDoc}
225      */
226   public void info(final Object msg, final Throwable t) {
227     log(INFO_STR, msg.toString(), t);
228   }
229 
230     /**
231      * {@inheritDoc}
232      */
233   public boolean isWarnEnabled() {
234     return true;
235   }
236 
237     /**
238      * {@inheritDoc}
239      */
240   public void warn(final Object msg) {
241     log(WARN_STR, msg.toString(), null);
242   }
243 
244     /**
245      * {@inheritDoc}
246      */
247   public void warn(final Object parameterizedMsg, final Object param1) {
248     parameterizedLog(WARN_STR, parameterizedMsg, param1, null);
249   }
250 
251     /**
252      * {@inheritDoc}
253      */
254   public void warn(final String parameterizedMsg,
255                    final Object param1,
256                    final Object param2) {
257     parameterizedLog(WARN_STR, parameterizedMsg, param1, param2);
258   }
259 
260     /**
261      * {@inheritDoc}
262      */
263   public void warn(final Object msg, final Throwable t) {
264     log(WARN_STR, msg.toString(), t);
265   }
266 
267     /**
268      * {@inheritDoc}
269      */
270   public boolean isErrorEnabled() {
271     return true;
272   }
273 
274     /**
275      * {@inheritDoc}
276      */
277   public void error(final Object msg) {
278     log(ERROR_STR, msg.toString(), null);
279   }
280 
281 
282     /**
283      * {@inheritDoc}
284      */
285   public void error(final Object parameterizedMsg, final Object param1) {
286     parameterizedLog(ERROR_STR, parameterizedMsg, param1, null);
287   }
288 
289     /**
290      * {@inheritDoc}
291      */
292   public void error(final String parameterizedMsg,
293                     final Object param1,
294                     final Object param2) {
295     parameterizedLog(ERROR_STR, parameterizedMsg, param1, param2);
296   }
297 
298     /**
299      * {@inheritDoc}
300      */
301   public void error(final Object msg, final Throwable t) {
302     log(ERROR_STR, msg.toString(), t);
303   }
304 
305 }