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 package org.apache.logging.log4j.core.config;
018
019 import org.apache.logging.log4j.Level;
020 import org.apache.logging.log4j.core.config.plugins.Plugin;
021 import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
022 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
023 import org.apache.logging.log4j.core.util.Assert;
024 import org.apache.logging.log4j.status.StatusLogger;
025
026 /**
027 * Descriptor of a custom Level object that is created via configuration.
028 */
029 @Plugin(name = "CustomLevel", category = "Core", printObject = true)
030 public final class CustomLevelConfig {
031
032 private final String levelName;
033 private final int intLevel;
034
035 private CustomLevelConfig(final String levelName, final int intLevel) {
036 this.levelName = Assert.requireNonNull(levelName, "levelName is null");
037 this.intLevel = intLevel;
038 }
039
040 /**
041 * Creates a CustomLevelConfig object. This also defines the Level object with a call to
042 * {@link Level#forName(String, int)}.
043 *
044 * @param levelName name of the custom level.
045 * @param intLevel the intLevel that determines where this level resides relative to the built-in levels
046 * @return A CustomLevelConfig object.
047 */
048 @PluginFactory
049 public static CustomLevelConfig createLevel(// @formatter:off
050 @PluginAttribute("name") final String levelName,
051 @PluginAttribute("intLevel") final int intLevel) {
052 // @formatter:on
053
054 StatusLogger.getLogger().debug("Creating CustomLevel(name='{}', intValue={})", levelName, intLevel);
055 Level.forName(levelName, intLevel);
056 return new CustomLevelConfig(levelName, intLevel);
057 }
058
059 /**
060 * Returns the custom level name.
061 *
062 * @return the custom level name
063 */
064 public String getLevelName() {
065 return levelName;
066 }
067
068 /**
069 * Returns the custom level intLevel that determines the strength of the custom level relative to the built-in
070 * levels.
071 *
072 * @return the custom level intLevel
073 */
074 public int getIntLevel() {
075 return intLevel;
076 }
077
078 @Override
079 public int hashCode() {
080 return intLevel ^ levelName.hashCode();
081 }
082
083 @Override
084 public boolean equals(final Object object) {
085 if (this == object) {
086 return true;
087 }
088 if (!(object instanceof CustomLevelConfig)) {
089 return false;
090 }
091 final CustomLevelConfig other = (CustomLevelConfig) object;
092 return this.intLevel == other.intLevel && this.levelName.equals(other.levelName);
093 }
094
095 @Override
096 public String toString() {
097 return "CustomLevel[name=" + levelName + ", intLevel=" + intLevel + "]";
098 }
099 }