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.Core; 025import org.apache.logging.log4j.core.LogEvent; 026import org.apache.logging.log4j.core.config.plugins.Plugin; 027import org.apache.logging.log4j.core.config.plugins.PluginAttribute; 028import org.apache.logging.log4j.core.config.plugins.PluginElement; 029import org.apache.logging.log4j.core.config.plugins.PluginFactory; 030import org.apache.logging.log4j.core.impl.Log4jLogEvent; 031import org.apache.logging.log4j.core.util.KeyValuePair; 032 033/** 034 * Rewrites log event levels for a given logger name. 035 * 036 * @since 2.4 037 */ 038@Plugin(name = "LoggerNameLevelRewritePolicy", category = Core.CATEGORY_NAME, elementType = "rewritePolicy", printObject = true) 039public class LoggerNameLevelRewritePolicy implements RewritePolicy { 040 041 /** 042 * Creates a policy to rewrite levels for a given logger name. 043 * 044 * @param loggerNamePrefix 045 * The logger name prefix for events to rewrite; all event logger names that start with this string will be 046 * rewritten. 047 * @param levelPairs 048 * The levels to rewrite, the key is the source level, the value the target level. 049 * @return a new LoggerNameLevelRewritePolicy 050 */ 051 @PluginFactory 052 public static LoggerNameLevelRewritePolicy createPolicy( 053 // @formatter:off 054 @PluginAttribute("logger") final String loggerNamePrefix, 055 @PluginElement("KeyValuePair") final KeyValuePair[] levelPairs) { 056 // @formatter:on 057 final Map<Level, Level> newMap = new HashMap<>(levelPairs.length); 058 for (final KeyValuePair keyValuePair : levelPairs) { 059 newMap.put(getLevel(keyValuePair.getKey()), getLevel(keyValuePair.getValue())); 060 } 061 return new LoggerNameLevelRewritePolicy(loggerNamePrefix, newMap); 062 } 063 064 private static Level getLevel(final String name) { 065 return Level.getLevel(name.toUpperCase(Locale.ROOT)); 066 } 067 068 private final String loggerName; 069 070 private final Map<Level, Level> map; 071 072 private LoggerNameLevelRewritePolicy(final String loggerName, final Map<Level, Level> map) { 073 super(); 074 this.loggerName = loggerName; 075 this.map = map; 076 } 077 078 @Override 079 public LogEvent rewrite(final LogEvent event) { 080 if (event.getLoggerName() == null || !event.getLoggerName().startsWith(loggerName)) { 081 return event; 082 } 083 final Level sourceLevel = event.getLevel(); 084 final Level newLevel = map.get(sourceLevel); 085 if (newLevel == null || newLevel == sourceLevel) { 086 return event; 087 } 088 final LogEvent result = new Log4jLogEvent.Builder(event).setLevel(newLevel).build(); 089 return result; 090 } 091 092}