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    
018    package org.apache.logging.log4j.jul;
019    
020    import org.apache.logging.log4j.Level;
021    import org.apache.logging.log4j.Logger;
022    import org.apache.logging.log4j.status.StatusLogger;
023    import org.apache.logging.log4j.util.LoaderUtil;
024    import org.apache.logging.log4j.util.PropertiesUtil;
025    
026    /**
027     * Utility class to convert between JDK Levels and Log4j 2 Levels.
028     *
029     * @since 2.1
030     */
031    public final class LevelTranslator {
032    
033        /**
034         * Custom Log4j level corresponding to the {@link java.util.logging.Level#FINEST} logging level. This maps to a
035         * level more specific than {@link org.apache.logging.log4j.Level#TRACE}.
036         */
037        public static final Level FINEST = Level.forName("FINEST", Level.TRACE.intLevel() + 100);
038    
039        /**
040         * Custom Log4j level corresponding to the {@link java.util.logging.Level#CONFIG} logging level. This maps to a
041         * level in between {@link org.apache.logging.log4j.Level#INFO} and {@link org.apache.logging.log4j.Level#DEBUG}.
042         */
043        public static final Level CONFIG = Level.forName("CONFIG", Level.INFO.intLevel() + 50);
044    
045        private static final Logger LOGGER = StatusLogger.getLogger();
046        private static final LevelConverter LEVEL_CONVERTER;
047    
048        static {
049            final String levelConverterClassName =
050                PropertiesUtil.getProperties().getStringProperty(Constants.LEVEL_CONVERTER_PROPERTY);
051            if (levelConverterClassName != null) {
052                LevelConverter levelConverter;
053                try {
054                    levelConverter = LoaderUtil.newCheckedInstanceOf(levelConverterClassName, LevelConverter.class);
055                } catch (final Exception e) {
056                    LOGGER.error("Could not create custom LevelConverter [{}].", levelConverterClassName, e);
057                    levelConverter = new DefaultLevelConverter();
058                }
059                LEVEL_CONVERTER = levelConverter;
060            } else {
061                LEVEL_CONVERTER = new DefaultLevelConverter();
062            }
063        }
064    
065        /**
066         * Converts a JDK logging Level to a Log4j logging Level.
067         *
068         * @param level JDK Level to convert.
069         * @return converted Level.
070         */
071        public static Level toLevel(final java.util.logging.Level level) {
072            return LEVEL_CONVERTER.toLevel(level);
073        }
074    
075        /**
076         * Converts a Log4j logging Level to a JDK logging Level.
077         *
078         * @param level Log4j Level to convert.
079         * @return converted Level.
080         */
081        public static java.util.logging.Level toJavaLevel(final Level level) {
082            return LEVEL_CONVERTER.toJavaLevel(level);
083        }
084    
085        private LevelTranslator() {
086        }
087    }