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 java.util.HashSet; 020import java.util.Map; 021import java.util.Set; 022import java.util.WeakHashMap; 023import java.util.concurrent.ConcurrentHashMap; 024import java.util.concurrent.ConcurrentMap; 025import java.util.concurrent.locks.ReadWriteLock; 026import java.util.concurrent.locks.ReentrantReadWriteLock; 027 028import org.apache.logging.log4j.LogManager; 029import org.apache.logging.log4j.util.LoaderUtil; 030 031/** 032 * Provides an abstract base class to use for implementing LoggerAdapter. 033 * 034 * @param <L> the Logger class to adapt 035 * @since 2.1 036 */ 037public abstract class AbstractLoggerAdapter<L> implements LoggerAdapter<L>, LoggerContextShutdownAware { 038 039 /** 040 * A map to store loggers for their given LoggerContexts. 041 */ 042 protected final Map<LoggerContext, ConcurrentMap<String, L>> registry = new ConcurrentHashMap<>(); 043 044 private final ReadWriteLock lock = new ReentrantReadWriteLock (true); 045 046 @Override 047 public L getLogger(final String name) { 048 final LoggerContext context = getContext(); 049 final ConcurrentMap<String, L> loggers = getLoggersInContext(context); 050 final L logger = loggers.get(name); 051 if (logger != null) { 052 return logger; 053 } 054 loggers.putIfAbsent(name, newLogger(name, context)); 055 return loggers.get(name); 056 } 057 058 @Override 059 public void contextShutdown(LoggerContext loggerContext) { 060 registry.remove(loggerContext); 061 } 062 063 /** 064 * Gets or creates the ConcurrentMap of named loggers for a given LoggerContext. 065 * 066 * @param context the LoggerContext to get loggers for 067 * @return the map of loggers for the given LoggerContext 068 */ 069 public ConcurrentMap<String, L> getLoggersInContext(final LoggerContext context) { 070 ConcurrentMap<String, L> loggers; 071 lock.readLock ().lock (); 072 try { 073 loggers = registry.get (context); 074 } finally { 075 lock.readLock ().unlock (); 076 } 077 078 if (loggers != null) { 079 return loggers; 080 } 081 lock.writeLock ().lock (); 082 try { 083 loggers = registry.get (context); 084 if (loggers == null) { 085 loggers = new ConcurrentHashMap<> (); 086 registry.put (context, loggers); 087 if (context instanceof LoggerContextShutdownEnabled) { 088 ((LoggerContextShutdownEnabled) context).addShutdownListener(this); 089 } 090 } 091 return loggers; 092 } finally { 093 lock.writeLock ().unlock (); 094 } 095 } 096 097 /** 098 * For unit testing. Consider to be private. 099 */ 100 public Set<LoggerContext> getLoggerContexts() { 101 return new HashSet<>(registry.keySet()); 102 } 103 104 /** 105 * Creates a new named logger for a given {@link LoggerContext}. 106 * 107 * @param name the name of the logger to create 108 * @param context the LoggerContext this logger will be associated with 109 * @return the new named logger 110 */ 111 protected abstract L newLogger(final String name, final LoggerContext context); 112 113 /** 114 * Gets the {@link LoggerContext} that should be used to look up or create loggers. This is similar in spirit to the 115 * {@code ContextSelector} class in {@code log4j-core}. However, implementations can rely on their own framework's 116 * separation of contexts instead (or simply use a singleton). 117 * 118 * @return the LoggerContext to be used for lookup and creation purposes 119 * @see org.apache.logging.log4j.LogManager#getContext(ClassLoader, boolean) 120 * @see org.apache.logging.log4j.LogManager#getContext(String, boolean) 121 */ 122 protected abstract LoggerContext getContext(); 123 124 /** 125 * Gets the {@link LoggerContext} associated with the given caller class. 126 * 127 * @param callerClass the caller class 128 * @return the LoggerContext for the calling class 129 */ 130 protected LoggerContext getContext(final Class<?> callerClass) { 131 ClassLoader cl = null; 132 if (callerClass != null) { 133 cl = callerClass.getClassLoader(); 134 } 135 if (cl == null) { 136 cl = LoaderUtil.getThreadContextClassLoader(); 137 } 138 return LogManager.getContext(cl, false); 139 } 140 141 @Override 142 public void close() { 143 lock.writeLock ().lock (); 144 try { 145 registry.clear(); 146 } finally { 147 lock.writeLock ().unlock (); 148 } 149 } 150}