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