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.core.async;
18
19 import java.net.URI;
20
21 import org.apache.logging.log4j.core.LoggerContext;
22 import org.apache.logging.log4j.core.selector.ClassLoaderContextSelector;
23 import org.apache.logging.log4j.core.util.Constants;
24 import org.apache.logging.log4j.util.PropertiesUtil;
25
26 /**
27 * {@code ContextSelector} that manages {@code AsyncLoggerContext} instances.
28 * <p>
29 * As of version 2.5, this class extends ClassLoaderContextSelector for better web app support.
30 */
31 public class AsyncLoggerContextSelector extends ClassLoaderContextSelector {
32
33 /**
34 * Returns {@code true} if the user specified this selector as the Log4jContextSelector, to make all loggers
35 * asynchronous.
36 *
37 * @return {@code true} if all loggers are asynchronous, {@code false} otherwise.
38 */
39 public static boolean isSelected() {
40 return AsyncLoggerContextSelector.class.getName().equals(
41 PropertiesUtil.getProperties().getStringProperty(Constants.LOG4J_CONTEXT_SELECTOR));
42 }
43
44 @Override
45 protected LoggerContext createContext(final String name, final URI configLocation) {
46 return new AsyncLoggerContext(name, null, configLocation);
47 }
48
49 @Override
50 protected String toContextMapKey(final ClassLoader loader) {
51 // LOG4J2-666 ensure unique name across separate instances created by webapp classloaders
52 return "AsyncContext@" + Integer.toHexString(System.identityHashCode(loader));
53 }
54
55 @Override
56 protected String defaultContextName() {
57 return "DefaultAsyncContext@" + Thread.currentThread().getName();
58 }
59 }