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