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