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