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 java.util.Map;
20  import java.util.WeakHashMap;
21  import java.util.concurrent.ConcurrentHashMap;
22  import java.util.concurrent.ConcurrentMap;
23  
24  import org.apache.logging.log4j.LogManager;
25  import org.apache.logging.log4j.util.LoaderUtil;
26  
27  /**
28   * Provides an abstract base class to use for implementing LoggerAdapter.
29   * @param <L> the Logger class to adapt
30   * @since 2.1
31   */
32  public abstract class AbstractLoggerAdapter<L> implements LoggerAdapter<L> {
33  
34      /**
35       * A map to store loggers for their given LoggerContexts.
36       */
37      protected final Map<LoggerContext, ConcurrentMap<String, L>> registry =
38          new WeakHashMap<LoggerContext, ConcurrentMap<String, L>>();
39  
40      @Override
41      public L getLogger(final String name) {
42          final LoggerContext context = getContext();
43          final ConcurrentMap<String, L> loggers = getLoggersInContext(context);
44          if (loggers.containsKey(name)) {
45              return loggers.get(name);
46          }
47          loggers.putIfAbsent(name, newLogger(name, context));
48          return loggers.get(name);
49      }
50  
51      /**
52       * Gets or creates the ConcurrentMap of named loggers for a given LoggerContext.
53       *
54       * @param context the LoggerContext to get loggers for
55       * @return the map of loggers for the given LoggerContext
56       */
57      public ConcurrentMap<String, L> getLoggersInContext(final LoggerContext context) {
58          synchronized (registry) {
59              ConcurrentMap<String, L> loggers = registry.get(context);
60              if (loggers == null) {
61                  loggers = new ConcurrentHashMap<String, L>();
62                  registry.put(context, loggers);
63              }
64              return loggers;
65          }
66      }
67  
68      /**
69       * Creates a new named logger for a given {@link LoggerContext}.
70       *
71       * @param name    the name of the logger to create
72       * @param context the LoggerContext this logger will be associated with
73       * @return the new named logger
74       */
75      protected abstract L newLogger(final String name, final LoggerContext context);
76  
77      /**
78       * Gets the {@link LoggerContext} that should be used to look up or create loggers. This is similar in spirit to
79       * the {@code ContextSelector} class in {@code log4j-core}. However, implementations can rely on their own
80       * framework's separation of contexts instead (or simply use a singleton).
81       *
82       * @return the LoggerContext to be used for lookup and creation purposes
83       * @see org.apache.logging.log4j.LogManager#getContext(ClassLoader, boolean)
84       * @see org.apache.logging.log4j.LogManager#getContext(String, boolean)
85       */
86      protected abstract LoggerContext getContext();
87  
88      /**
89       * Gets the {@link LoggerContext} associated with the given caller class.
90       *
91       * @param callerClass the caller class
92       * @return the LoggerContext for the calling class
93       */
94      protected LoggerContext getContext(final Class<?> callerClass) {
95          ClassLoader cl = null;
96          if (callerClass != null) {
97              cl = callerClass.getClassLoader();
98          }
99          if (cl == null) {
100             cl = LoaderUtil.getThreadContextClassLoader();
101         }
102         return LogManager.getContext(cl, false);
103     }
104 
105     @Override
106     public void close() {
107         registry.clear();
108     }
109 }