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.core.pattern;
18  
19  import org.apache.logging.log4j.Level;
20  import org.apache.logging.log4j.core.LogEvent;
21  import org.apache.logging.log4j.core.config.plugins.Plugin;
22  
23  import java.util.EnumMap;
24  
25  /**
26   * Returns the event's level in a StringBuffer.
27   */
28  @Plugin(name = "LevelPatternConverter", category = "Converter")
29  @ConverterKeys({"p", "level" })
30  public final class LevelPatternConverter extends LogEventPatternConverter {
31      /**
32       * Singleton.
33       */
34      private static final LevelPatternConverter INSTANCE = new LevelPatternConverter(null);
35  
36      private final EnumMap<Level, String> levelMap;
37  
38      /**
39       * Private constructor.
40       */
41      private LevelPatternConverter(final EnumMap<Level, String> map) {
42          super("Level", "level");
43          this.levelMap = map;
44      }
45  
46      /**
47       * Obtains an instance of pattern converter.
48       *
49       * @param options options, may be null. May contain a list of level names and
50       * The value that should be displayed for the Level.
51       * @return instance of pattern converter.
52       */
53      public static LevelPatternConverter newInstance(final String[] options) {
54          if (options == null || options.length == 0) {
55              return INSTANCE;
56          }
57          final EnumMap<Level, String> levelMap = new EnumMap<Level, String>(Level.class);
58          final String[] definitions = options[0].split(",");
59          for (final String def : definitions) {
60              final String[] pair = def.split("=");
61              if (pair == null || pair.length != 2) {
62                  LOGGER.error("Invalid option {}", def);
63                  continue;
64              }
65              final Level level = Level.toLevel(pair[0].trim(), null);
66              if (level == null) {
67                  LOGGER.error("Invalid Level {}", pair[0].trim());
68              } else {
69                  levelMap.put(level, pair[1].trim());
70              }
71          }
72          if (levelMap.size() == 0) {
73              return INSTANCE;
74          }
75          for (final Level level : Level.values()) {
76              if (!levelMap.containsKey(level)) {
77                  levelMap.put(level, level.toString());
78              }
79          }
80          return new LevelPatternConverter(levelMap);
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public void format(final LogEvent event, final StringBuilder output) {
88          output.append(levelMap == null ? event.getLevel().toString() : levelMap.get(event.getLevel()));
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public String getStyleClass(final Object e) {
96          if (e instanceof LogEvent) {
97              final Level level = ((LogEvent) e).getLevel();
98  
99              switch (level) {
100                 case TRACE:
101                     return "level trace";
102 
103                 case DEBUG:
104                     return "level debug";
105 
106                 case INFO:
107                     return "level info";
108 
109                 case WARN:
110                     return "level warn";
111 
112                 case ERROR:
113                     return "level error";
114 
115                 case FATAL:
116                     return "level fatal";
117 
118                 default:
119                     return "level " + ((LogEvent) e).getLevel().toString();
120             }
121         }
122 
123         return "level";
124     }
125 }