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