001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.logging.log4j.core.appender.rewrite; 018 019import java.util.HashMap; 020import java.util.Locale; 021import java.util.Map; 022 023import org.apache.logging.log4j.Level; 024import org.apache.logging.log4j.core.LogEvent; 025import org.apache.logging.log4j.core.config.plugins.Plugin; 026import org.apache.logging.log4j.core.config.plugins.PluginAttribute; 027import org.apache.logging.log4j.core.config.plugins.PluginElement; 028import org.apache.logging.log4j.core.config.plugins.PluginFactory; 029import org.apache.logging.log4j.core.impl.Log4jLogEvent; 030import org.apache.logging.log4j.core.util.KeyValuePair; 031 032/** 033 * Rewrites log event levels for a given logger name. 034 * 035 * @since 2.4 036 */ 037@Plugin(name = "LoggerNameLevelRewritePolicy", category = "Core", elementType = "rewritePolicy", printObject = true) 038public class LoggerNameLevelRewritePolicy implements RewritePolicy { 039 040 /** 041 * Creates a policy to rewrite levels for a given logger name. 042 * 043 * @param loggerNamePrefix 044 * The logger name prefix for events to rewrite; all event logger names that start with this string will be 045 * rewritten. 046 * @param levelPairs 047 * The levels to rewrite, the key is the source level, the value the target level. 048 * @return a new LoggerNameLevelRewritePolicy 049 */ 050 @PluginFactory 051 public static LoggerNameLevelRewritePolicy createPolicy( 052 // @formatter:off 053 @PluginAttribute("logger") final String loggerNamePrefix, 054 @PluginElement("KeyValuePair") final KeyValuePair[] levelPairs) { 055 // @formatter:on 056 final Map<Level, Level> newMap = new HashMap<>(levelPairs.length); 057 for (final KeyValuePair keyValuePair : levelPairs) { 058 newMap.put(getLevel(keyValuePair.getKey()), getLevel(keyValuePair.getValue())); 059 } 060 return new LoggerNameLevelRewritePolicy(loggerNamePrefix, newMap); 061 } 062 063 private static Level getLevel(final String name) { 064 return Level.getLevel(name.toUpperCase(Locale.ROOT)); 065 } 066 067 private final String loggerName; 068 069 private final Map<Level, Level> map; 070 071 private LoggerNameLevelRewritePolicy(final String loggerName, final Map<Level, Level> map) { 072 super(); 073 this.loggerName = loggerName; 074 this.map = map; 075 } 076 077 @Override 078 public LogEvent rewrite(final LogEvent event) { 079 if (!event.getLoggerName().startsWith(loggerName)) { 080 return event; 081 } 082 final Level sourceLevel = event.getLevel(); 083 final Level newLevel = map.get(sourceLevel); 084 if (newLevel == null || newLevel == sourceLevel) { 085 return event; 086 } 087 final LogEvent result = new Log4jLogEvent.Builder(event).setLevel(newLevel).build(); 088 return result; 089 } 090 091}