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", category = "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                } else {
069                    levelMap.put(level, pair[1].trim());
070                }
071            }
072            if (levelMap.size() == 0) {
073                return INSTANCE;
074            }
075            for (final Level level : Level.values()) {
076                if (!levelMap.containsKey(level)) {
077                    levelMap.put(level, level.toString());
078                }
079            }
080            return new LevelPatternConverter(levelMap);
081        }
082    
083        /**
084         * {@inheritDoc}
085         */
086        @Override
087        public void format(final LogEvent event, final StringBuilder output) {
088            output.append(levelMap == null ? event.getLevel().toString() : levelMap.get(event.getLevel()));
089        }
090    
091        /**
092         * {@inheritDoc}
093         */
094        @Override
095        public String getStyleClass(final Object e) {
096            if (e instanceof LogEvent) {
097                final Level level = ((LogEvent) e).getLevel();
098    
099                switch (level) {
100                    case TRACE:
101                        return "level trace";
102    
103                    case DEBUG:
104                        return "level debug";
105    
106                    case INFO:
107                        return "level info";
108    
109                    case WARN:
110                        return "level warn";
111    
112                    case ERROR:
113                        return "level error";
114    
115                    case FATAL:
116                        return "level fatal";
117    
118                    default:
119                        return "level " + ((LogEvent) e).getLevel().toString();
120                }
121            }
122    
123            return "level";
124        }
125    }