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", type = "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              }
69              levelMap.put(level, pair[1].trim());
70          }
71          if (levelMap.size() == 0) {
72              return INSTANCE;
73          }
74          for (final Level level : Level.values()) {
75              if (!levelMap.containsKey(level)) {
76                  levelMap.put(level, level.toString());
77              }
78          }
79          return new LevelPatternConverter(levelMap);
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      @Override
86      public void format(final LogEvent event, final StringBuilder output) {
87          output.append(levelMap == null ? event.getLevel().toString() : levelMap.get(event.getLevel()));
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      @Override
94      public String getStyleClass(final Object e) {
95          if (e instanceof LogEvent) {
96              final Level level = ((LogEvent) e).getLevel();
97  
98              switch (level) {
99                  case TRACE:
100                     return "level trace";
101 
102                 case DEBUG:
103                     return "level debug";
104 
105                 case INFO:
106                     return "level info";
107 
108                 case WARN:
109                     return "level warn";
110 
111                 case ERROR:
112                     return "level error";
113 
114                 case FATAL:
115                     return "level fatal";
116 
117                 default:
118                     return "level " + ((LogEvent) e).getLevel().toString();
119             }
120         }
121 
122         return "level";
123     }
124 }