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