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