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