View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.spi;
18  
19  import org.apache.logging.log4j.message.MessageFactory;
20  
21  /**
22   * Anchor point for logging implementations.
23   */
24  public interface LoggerContext {
25  
26      /**
27       * An anchor for some other context, such as a ClassLoader or ServletContext.
28       * @return The external context.
29       */
30      Object getExternalContext();
31  
32      /**
33       * Retrieve an object by its name.
34       * @param key The object's key.
35       * @since 2.13.0
36       */
37      default Object getObject(String key) {
38          return null;
39      }
40  
41      /**
42       * Store an object into the LoggerContext by name for later use.
43       * @param key The object's key.
44       * @param value The object.
45       * @return The previous object or null.
46       * @since 2.13.0
47       */
48      default Object putObject(String key, Object value) {
49          return null;
50      }
51  
52      /**
53       * Store an object into the LoggerContext by name for later use if an object is not already stored with that key.
54       * @param key The object's key.
55       * @param value The object.
56       * @return The previous object or null.
57       * @since 2.13.0
58       */
59      default Object putObjectIfAbsent(String key, Object value) {
60          return null;
61      }
62  
63      /**
64       * Remove an object if it is present.
65       * @param key The object's key.
66       * @return The object if it was present, null if it was not.
67       * @since 2.13.0
68       */
69      default Object removeObject(String key) {
70          return null;
71      }
72  
73      /**
74       * Remove an object if it is present and the provided object is stored.
75       * @param key The object's key.
76       * @param value The object.
77       * @return The object if it was present, null if it was not.
78       * @since 2.13.0
79       */
80      default boolean removeObject(String key, Object value) {
81          return false;
82      }
83  
84      /**
85       * Returns an ExtendedLogger.
86       * @param name The name of the Logger to return.
87       * @return The logger with the specified name.
88       */
89      ExtendedLogger getLogger(String name);
90  
91      /**
92       * Returns an ExtendedLogger using the fully qualified name of the Class as the Logger name.
93       * @param cls The Class whose name should be used as the Logger name.
94       * @return The logger.
95       * @since 2.14.0
96       */
97      default ExtendedLogger getLogger(Class<?> cls) {
98          final String canonicalName = cls.getCanonicalName();
99          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 }