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  
18  package org.apache.log4j.pattern;
19  
20  import org.apache.log4j.Level;
21  import org.apache.log4j.spi.LoggingEvent;
22  
23  
24  /**
25   * Return the event's level in a StringBuffer.
26   *
27   * @author Ceki Gülcü
28   */
29  public final class LevelPatternConverter extends LoggingEventPatternConverter {
30  
31    /**
32     *   Integer severity for Level.TRACE.
33     */
34    private static final int TRACE_INT = 5000;
35    /**
36     * Singleton.
37     */
38    private static final LevelPatternConverter INSTANCE =
39      new LevelPatternConverter();
40  
41    /**
42     * Private constructor.
43     */
44    private LevelPatternConverter() {
45      super("Level", "level");
46    }
47  
48    /**
49     * Obtains an instance of pattern converter.
50     * @param options options, may be null.
51     * @return instance of pattern converter.
52     */
53    public static LevelPatternConverter newInstance(
54      final String[] options) {
55      return INSTANCE;
56    }
57  
58    /**
59     * {@inheritDoc}
60     */
61    public void format(final LoggingEvent event, final StringBuffer output) {
62      output.append(event.getLevel().toString());
63    }
64  
65    /**
66     * {@inheritDoc}
67     */
68    public String getStyleClass(Object e) {
69      if (e instanceof LoggingEvent) {
70        int lint = ((LoggingEvent) e).getLevel().toInt();
71  
72        switch (lint) {
73        case TRACE_INT:
74          return "level trace";
75  
76        case Level.DEBUG_INT:
77          return "level debug";
78  
79        case Level.INFO_INT:
80          return "level info";
81  
82        case Level.WARN_INT:
83          return "level warn";
84  
85        case Level.ERROR_INT:
86          return "level error";
87  
88        case Level.FATAL_INT:
89          return "level fatal";
90  
91        default:
92          return "level " + ((LoggingEvent) e).getLevel().toString();
93        }
94      }
95  
96      return "level";
97    }
98  }