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