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  package org.apache.logging.log4j.core.config;
    18
    19  import org.apache.logging.log4j.Level;
    20  import org.apache.logging.log4j.core.config.plugins.Plugin;
    21  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
    22  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
    23  import org.apache.logging.log4j.core.util.Assert;
    24  import org.apache.logging.log4j.status.StatusLogger;
    25
    26  /**
    27   * Descriptor of a custom Level object that is created via configuration.
    28   */
    29  @Plugin(name = "CustomLevel", category = "Core", printObject = true)
    30  public final class CustomLevelConfig {
    31
    32      private final String levelName;
    33      private final int intLevel;
    34
    35      private CustomLevelConfig(final String levelName, final int intLevel) {
    36          this.levelName = Assert.requireNonNull(levelName, "levelName is null");
    37          this.intLevel = intLevel;
    38      }
    39
    40      /**
    41       * Creates a CustomLevelConfig object. This also defines the Level object with a call to
    42       * {@link Level#forName(String, int)}.
    43       *
    44       * @param levelName name of the custom level.
    45       * @param intLevel the intLevel that determines where this level resides relative to the built-in levels
    46       * @return A CustomLevelConfig object.
    47       */
    48      @PluginFactory
    49      public static CustomLevelConfig createLevel(// @formatter:off
    50              @PluginAttribute("name") final String levelName,
    51              @PluginAttribute("intLevel") final int intLevel) {
    52          // @formatter:on
    53
    54          StatusLogger.getLogger().debug("Creating CustomLevel(name='{}', intValue={})", levelName, intLevel);
    55          Level.forName(levelName, intLevel);
    56          return new CustomLevelConfig(levelName, intLevel);
    57      }
    58
    59      /**
    60       * Returns the custom level name.
    61       *
    62       * @return the custom level name
    63       */
    64      public String getLevelName() {
    65          return levelName;
    66      }
    67
    68      /**
    69       * Returns the custom level intLevel that determines the strength of the custom level relative to the built-in
    70       * levels.
    71       *
    72       * @return the custom level intLevel
    73       */
    74      public int getIntLevel() {
    75          return intLevel;
    76      }
    77
    78      @Override
    79      public int hashCode() {
    80          return intLevel ^ levelName.hashCode();
    81      }
    82
    83      @Override
    84      public boolean equals(final Object object) {
    85          if (this == object) {
    86              return true;
    87          }
    88          if (!(object instanceof CustomLevelConfig)) {
    89              return false;
    90          }
    91          final CustomLevelConfig other = (CustomLevelConfig) object;
    92          return this.intLevel == other.intLevel && this.levelName.equals(other.levelName);
    93      }
    94
    95      @Override
    96      public String toString() {
    97          return "CustomLevel[name=" + levelName + ", intLevel=" + intLevel + "]";
    98      }
    99  }