|
16 | 16 | */
|
17 | 17 | package org.apache.logging.log4j.util;
|
18 | 18 |
|
| 19 | +import org.apache.logging.log4j.spi.Provider; |
| 20 | + |
19 | 21 | /**
|
20 | 22 | * Log4j API Constants.
|
21 | 23 | *
|
22 | 24 | * @since 2.6.2
|
23 | 25 | */
|
24 | 26 | public final class Constants {
|
25 | 27 | /**
|
26 |
| - * {@code true} if we think we are running in a web container, based on the boolean value of system property |
27 |
| - * "log4j2.is.webapp", or (if this system property is not set) whether the {@code javax.servlet.Servlet} class |
28 |
| - * is present in the classpath. |
| 28 | + * Specifies whether Log4j is used in a servlet container |
| 29 | + * <p> |
| 30 | + * If {@code true} Log4j disables the features, which are incompatible with a typical servlet application: |
| 31 | + * </p> |
| 32 | + * <ol> |
| 33 | + * <li>It disables the usage of {@link ThreadLocal}s for object pooling (unless the user has explicitly provided a {@link #ENABLE_THREADLOCALS} property)</li> |
| 34 | + * <li>It uses a web-application safe implementation of {@link org.apache.logging.log4j.spi.ThreadContextMap} |
| 35 | + * (see {@link Provider#getThreadContextMap()}),</li> |
| 36 | + * <li>It disables the shutdown hook,</li> |
| 37 | + * <li>It uses the caller thread to send JMX notifications.</li> |
| 38 | + * </ol> |
| 39 | + * <p> |
| 40 | + * The value of this constant depends upon the presence of the Servlet API on the classpath and can be |
| 41 | + * overridden using the {@code "log4j2.isWebapp"} system property. |
| 42 | + * </p> |
29 | 43 | */
|
30 | 44 | public static final boolean IS_WEB_APP = PropertiesUtil.getProperties()
|
31 | 45 | .getBooleanProperty(
|
32 | 46 | "log4j2.is.webapp",
|
33 | 47 | isClassAvailable("javax.servlet.Servlet") || isClassAvailable("jakarta.servlet.Servlet"));
|
34 | 48 |
|
35 | 49 | /**
|
36 |
| - * Kill switch for object pooling in ThreadLocals that enables much of the LOG4J2-1270 no-GC behaviour. |
| 50 | + * Specifies whether Log4j can bind non-JRE types to {@link ThreadLocal}s |
| 51 | + * <p> |
| 52 | + * The value of this constant is {@code true}, unless Log4j is running in a servlet container (cf. |
| 53 | + * {@link #IS_WEB_APP}). Use the {@code "log4j2.enableThreadlocals} system property to override its value. |
| 54 | + * </p> |
| 55 | + * <p> |
| 56 | + * In order to enable the garbage-free behavior described in |
| 57 | + * <a href="https://issues.apache.org/jira/browse/LOG4J2-1270">LOG4J2-1270</a>, this constant must be {@code |
| 58 | + * true}. |
| 59 | + * </p> |
37 | 60 | * <p>
|
38 |
| - * {@code True} for non-{@link #IS_WEB_APP web apps}, disable by setting system property |
39 |
| - * "log4j2.enable.threadlocals" to "false". |
| 61 | + * <strong>Warning:</strong> This setting does <strong>not</strong> disable all thread locals. It only |
| 62 | + * disables those thread locals that can cause a classloader memory leak. |
40 | 63 | * </p>
|
41 | 64 | */
|
42 | 65 | public static final boolean ENABLE_THREADLOCALS =
|
43 |
| - !IS_WEB_APP && PropertiesUtil.getProperties().getBooleanProperty("log4j2.enable.threadlocals", true); |
| 66 | + PropertiesUtil.getProperties().getBooleanProperty("log4j2.enable.threadlocals", !IS_WEB_APP); |
44 | 67 |
|
45 | 68 | /**
|
46 | 69 | * Java major version.
|
|
0 commit comments