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.spi; 018 019import org.apache.logging.log4j.message.MessageFactory; 020 021/** 022 * Anchor point for logging implementations. 023 */ 024public interface LoggerContext { 025 026 /** 027 * An anchor for some other context, such as a ClassLoader or ServletContext. 028 * @return The external context. 029 */ 030 Object getExternalContext(); 031 032 /** 033 * Retrieve an object by its name. 034 * @param key The object's key. 035 * @since 2.13.0 036 */ 037 default Object getObject(String key) { 038 return null; 039 } 040 041 /** 042 * Store an object into the LoggerContext by name for later use. 043 * @param key The object's key. 044 * @param value The object. 045 * @return The previous object or null. 046 * @since 2.13.0 047 */ 048 default Object putObject(String key, Object value) { 049 return null; 050 } 051 052 /** 053 * Store an object into the LoggerContext by name for later use if an object is not already stored with that key. 054 * @param key The object's key. 055 * @param value The object. 056 * @return The previous object or null. 057 * @since 2.13.0 058 */ 059 default Object putObjectIfAbsent(String key, Object value) { 060 return null; 061 } 062 063 /** 064 * Remove an object if it is present. 065 * @param key The object's key. 066 * @return The object if it was present, null if it was not. 067 * @since 2.13.0 068 */ 069 default Object removeObject(String key) { 070 return null; 071 } 072 073 /** 074 * Remove an object if it is present and the provided object is stored. 075 * @param key The object's key. 076 * @param value The object. 077 * @return The object if it was present, null if it was not. 078 * @since 2.13.0 079 */ 080 default boolean removeObject(String key, Object value) { 081 return false; 082 } 083 084 /** 085 * Returns an ExtendedLogger. 086 * @param name The name of the Logger to return. 087 * @return The logger with the specified name. 088 */ 089 ExtendedLogger getLogger(String name); 090 091 /** 092 * Returns an ExtendedLogger using the fully qualified name of the Class as the Logger name. 093 * @param cls The Class whose name should be used as the Logger name. 094 * @return The logger. 095 * @since 2.14.0 096 */ 097 default ExtendedLogger getLogger(Class<?> cls) { 098 final String canonicalName = cls.getCanonicalName(); 099 return getLogger(canonicalName != null ? canonicalName : cls.getName()); 100 } 101 102 /** 103 * Returns an ExtendedLogger. 104 * @param name The name of the Logger to return. 105 * @param messageFactory The message factory is used only when creating a logger, subsequent use does not change 106 * the logger but will log a warning if mismatched. 107 * @return The logger with the specified name. 108 */ 109 ExtendedLogger getLogger(String name, MessageFactory messageFactory); 110 111 /** 112 * Returns an ExtendedLogger using the fully qualified name of the Class as the Logger name. 113 * @param cls The Class whose name should be used as the Logger name. 114 * @param messageFactory The message factory is used only when creating a logger, subsequent use does not change the 115 * logger but will log a warning if mismatched. 116 * @return The logger. 117 * @since 2.14.0 118 */ 119 default ExtendedLogger getLogger(Class<?> cls, MessageFactory messageFactory) { 120 final String canonicalName = cls.getCanonicalName(); 121 return getLogger(canonicalName != null ? canonicalName : cls.getName(), messageFactory); 122 } 123 124 /** 125 * Detects if a Logger with the specified name exists. 126 * @param name The Logger name to search for. 127 * @return true if the Logger exists, false otherwise. 128 */ 129 boolean hasLogger(String name); 130 131 /** 132 * Detects if a Logger with the specified name and MessageFactory exists. 133 * @param name The Logger name to search for. 134 * @param messageFactory The message factory to search for. 135 * @return true if the Logger exists, false otherwise. 136 * @since 2.5 137 */ 138 boolean hasLogger(String name, MessageFactory messageFactory); 139 140 /** 141 * Detects if a Logger with the specified name and MessageFactory type exists. 142 * @param name The Logger name to search for. 143 * @param messageFactoryClass The message factory class to search for. 144 * @return true if the Logger exists, false otherwise. 145 * @since 2.5 146 */ 147 boolean hasLogger(String name, Class<? extends MessageFactory> messageFactoryClass); 148}