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 */ 017 package org.apache.logging.log4j.core.pattern; 018 019 import org.apache.logging.log4j.Level; 020 import org.apache.logging.log4j.core.LogEvent; 021 import org.apache.logging.log4j.core.config.plugins.Plugin; 022 023 import java.util.EnumMap; 024 025 /** 026 * Returns the event's level in a StringBuffer. 027 */ 028 @Plugin(name = "LevelPatternConverter", type = "Converter") 029 @ConverterKeys({"p", "level" }) 030 public final class LevelPatternConverter extends LogEventPatternConverter { 031 /** 032 * Singleton. 033 */ 034 private static final LevelPatternConverter INSTANCE = new LevelPatternConverter(null); 035 036 private final EnumMap<Level, String> levelMap; 037 038 /** 039 * Private constructor. 040 */ 041 private LevelPatternConverter(final EnumMap<Level, String> map) { 042 super("Level", "level"); 043 this.levelMap = map; 044 } 045 046 /** 047 * Obtains an instance of pattern converter. 048 * 049 * @param options options, may be null. May contain a list of level names and 050 * The value that should be displayed for the Level. 051 * @return instance of pattern converter. 052 */ 053 public static LevelPatternConverter newInstance(final String[] options) { 054 if (options == null || options.length == 0) { 055 return INSTANCE; 056 } 057 final EnumMap<Level, String> levelMap = new EnumMap<Level, String>(Level.class); 058 final String[] definitions = options[0].split(","); 059 for (final String def : definitions) { 060 final String[] pair = def.split("="); 061 if (pair == null || pair.length != 2) { 062 LOGGER.error("Invalid option {}", def); 063 continue; 064 } 065 final Level level = Level.toLevel(pair[0].trim(), null); 066 if (level == null) { 067 LOGGER.error("Invalid Level {}", pair[0].trim()); 068 } 069 levelMap.put(level, pair[1].trim()); 070 } 071 if (levelMap.size() == 0) { 072 return INSTANCE; 073 } 074 for (final Level level : Level.values()) { 075 if (!levelMap.containsKey(level)) { 076 levelMap.put(level, level.toString()); 077 } 078 } 079 return new LevelPatternConverter(levelMap); 080 } 081 082 /** 083 * {@inheritDoc} 084 */ 085 @Override 086 public void format(final LogEvent event, final StringBuilder output) { 087 output.append(levelMap == null ? event.getLevel().toString() : levelMap.get(event.getLevel())); 088 } 089 090 /** 091 * {@inheritDoc} 092 */ 093 @Override 094 public String getStyleClass(final Object e) { 095 if (e instanceof LogEvent) { 096 final Level level = ((LogEvent) e).getLevel(); 097 098 switch (level) { 099 case TRACE: 100 return "level trace"; 101 102 case DEBUG: 103 return "level debug"; 104 105 case INFO: 106 return "level info"; 107 108 case WARN: 109 return "level warn"; 110 111 case ERROR: 112 return "level error"; 113 114 case FATAL: 115 return "level fatal"; 116 117 default: 118 return "level " + ((LogEvent) e).getLevel().toString(); 119 } 120 } 121 122 return "level"; 123 } 124 }