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.appender.rewrite;
18  
19  import java.util.HashMap;
20  import java.util.Locale;
21  import java.util.Map;
22  
23  import org.apache.logging.log4j.Level;
24  import org.apache.logging.log4j.core.LogEvent;
25  import org.apache.logging.log4j.core.config.plugins.Plugin;
26  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
27  import org.apache.logging.log4j.core.config.plugins.PluginElement;
28  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
29  import org.apache.logging.log4j.core.impl.Log4jLogEvent;
30  import org.apache.logging.log4j.core.util.KeyValuePair;
31  
32  /**
33   * Rewrites log event levels for a given logger name.
34   * 
35   * @since 2.4
36   */
37  @Plugin(name = "LoggerNameLevelRewritePolicy", category = "Core", elementType = "rewritePolicy", printObject = true)
38  public class LoggerNameLevelRewritePolicy implements RewritePolicy {
39  
40      /**
41       * Creates a policy to rewrite levels for a given logger name.
42       * 
43       * @param loggerNamePrefix
44       *        The logger name prefix for events to rewrite; all event logger names that start with this string will be
45       *        rewritten.
46       * @param levelPairs
47       *        The levels to rewrite, the key is the source level, the value the target level.
48       * @return a new LoggerNameLevelRewritePolicy
49       */
50      @PluginFactory
51      public static LoggerNameLevelRewritePolicy createPolicy(
52              // @formatter:off
53              @PluginAttribute("logger") final String loggerNamePrefix,
54              @PluginElement("KeyValuePair") final KeyValuePair[] levelPairs) {
55              // @formatter:on
56          final Map<Level, Level> newMap = new HashMap<>(levelPairs.length);
57          for (final KeyValuePair keyValuePair : levelPairs) {
58              newMap.put(getLevel(keyValuePair.getKey()), getLevel(keyValuePair.getValue()));
59          }
60          return new LoggerNameLevelRewritePolicy(loggerNamePrefix, newMap);
61      }
62  
63      private static Level getLevel(final String name) {
64          return Level.getLevel(name.toUpperCase(Locale.ROOT));
65      }
66  
67      private final String loggerName;
68  
69      private final Map<Level, Level> map;
70  
71      private LoggerNameLevelRewritePolicy(final String loggerName, final Map<Level, Level> map) {
72          super();
73          this.loggerName = loggerName;
74          this.map = map;
75      }
76  
77      @Override
78      public LogEvent rewrite(final LogEvent event) {
79          if (!event.getLoggerName().startsWith(loggerName)) {
80              return event;
81          }
82          final Level sourceLevel = event.getLevel();
83          final Level newLevel = map.get(sourceLevel);
84          if (newLevel == null || newLevel == sourceLevel) {
85              return event;
86          }
87          final LogEvent result = new Log4jLogEvent.Builder(event).setLevel(newLevel).build();
88          return result;
89      }
90  
91  }