View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements. See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache license, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License. You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the license for the specific language governing permissions and
    15   * limitations under the license.
    16   */
    17
    18  package org.apache.logging.log4j.jul;
    19
    20  import org.apache.logging.log4j.Level;
    21  import org.apache.logging.log4j.Logger;
    22  import org.apache.logging.log4j.status.StatusLogger;
    23  import org.apache.logging.log4j.util.LoaderUtil;
    24  import org.apache.logging.log4j.util.PropertiesUtil;
    25
    26  /**
    27   * Utility class to convert between JDK Levels and Log4j 2 Levels.
    28   *
    29   * @since 2.1
    30   */
    31  public final class LevelTranslator {
    32
    33      /**
    34       * Custom Log4j level corresponding to the {@link java.util.logging.Level#FINEST} logging level. This maps to a
    35       * level more specific than {@link org.apache.logging.log4j.Level#TRACE}.
    36       */
    37      public static final Level FINEST = Level.forName("FINEST", Level.TRACE.intLevel() + 100);
    38
    39      /**
    40       * Custom Log4j level corresponding to the {@link java.util.logging.Level#CONFIG} logging level. This maps to a
    41       * level in between {@link org.apache.logging.log4j.Level#INFO} and {@link org.apache.logging.log4j.Level#DEBUG}.
    42       */
    43      public static final Level CONFIG = Level.forName("CONFIG", Level.INFO.intLevel() + 50);
    44
    45      private static final Logger LOGGER = StatusLogger.getLogger();
    46      private static final LevelConverter LEVEL_CONVERTER;
    47
    48      static {
    49          final String levelConverterClassName =
    50              PropertiesUtil.getProperties().getStringProperty(Constants.LEVEL_CONVERTER_PROPERTY);
    51          if (levelConverterClassName != null) {
    52              LevelConverter levelConverter;
    53              try {
    54                  levelConverter = LoaderUtil.newCheckedInstanceOf(levelConverterClassName, LevelConverter.class);
    55              } catch (final Exception e) {
    56                  LOGGER.error("Could not create custom LevelConverter [{}].", levelConverterClassName, e);
    57                  levelConverter = new DefaultLevelConverter();
    58              }
    59              LEVEL_CONVERTER = levelConverter;
    60          } else {
    61              LEVEL_CONVERTER = new DefaultLevelConverter();
    62          }
    63      }
    64
    65      /**
    66       * Converts a JDK logging Level to a Log4j logging Level.
    67       *
    68       * @param level JDK Level to convert.
    69       * @return converted Level.
    70       */
    71      public static Level toLevel(final java.util.logging.Level level) {
    72          return LEVEL_CONVERTER.toLevel(level);
    73      }
    74
    75      /**
    76       * Converts a Log4j logging Level to a JDK logging Level.
    77       *
    78       * @param level Log4j Level to convert.
    79       * @return converted Level.
    80       */
    81      public static java.util.logging.Level toJavaLevel(final Level level) {
    82          return LEVEL_CONVERTER.toJavaLevel(level);
    83      }
    84
    85      private LevelTranslator() {
    86      }
    87  }