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.core.async; 018 019import java.net.URI; 020 021import org.apache.logging.log4j.core.LoggerContext; 022import org.apache.logging.log4j.core.selector.ClassLoaderContextSelector; 023import org.apache.logging.log4j.core.util.Constants; 024import org.apache.logging.log4j.util.PropertiesUtil; 025 026/** 027 * {@code ContextSelector} that manages {@code AsyncLoggerContext} instances. 028 * <p> 029 * As of version 2.5, this class extends ClassLoaderContextSelector for better web app support. 030 */ 031public class AsyncLoggerContextSelector extends ClassLoaderContextSelector { 032 033 /** 034 * Returns {@code true} if the user specified this selector as the Log4jContextSelector, to make all loggers 035 * asynchronous. 036 * 037 * @return {@code true} if all loggers are asynchronous, {@code false} otherwise. 038 */ 039 public static boolean isSelected() { 040 return AsyncLoggerContextSelector.class.getName().equals( 041 PropertiesUtil.getProperties().getStringProperty(Constants.LOG4J_CONTEXT_SELECTOR)); 042 } 043 044 @Override 045 protected LoggerContext createContext(final String name, final URI configLocation) { 046 return new AsyncLoggerContext(name, null, configLocation); 047 } 048 049 @Override 050 protected String toContextMapKey(final ClassLoader loader) { 051 // LOG4J2-666 ensure unique name across separate instances created by webapp classloaders 052 return "AsyncContext@" + Integer.toHexString(System.identityHashCode(loader)); 053 } 054 055 @Override 056 protected String defaultContextName() { 057 return "DefaultAsyncContext@" + Thread.currentThread().getName(); 058 } 059}