You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: src/site/antora/modules/ROOT/pages/manual/eventlogging.adoc
+5-3
Original file line number
Diff line number
Diff line change
@@ -38,14 +38,16 @@ This feature allows for easy integration with existing log management and monito
38
38
39
39
== Example Configuration
40
40
41
-
To configure Log4j to output logs in Syslog (RFC5424) format, one needs to use the link:../javadoc/log4j-core/org/apache/logging/log4j/core/layout/StructuredDataLayout.html[`StructuredDataLayout`] layout.
41
+
To configure Log4j to output logs in Syslog (RFC5424) format, one needs to use the
LoggerContext factory]. They perform the actual work of locating or
71
+
link:../javadoc/log4j-core/org/apache/logging/log4j/core/impl/Log4jContextFactory.html[Log4j LoggerContext factory]. They perform the actual work of locating or
70
72
creating a LoggerContext, which is the anchor for Loggers and their
71
73
configuration. ContextSelectors are free to implement any mechanism they
72
74
desire to manage LoggerContexts. The default Log4jContextFactory checks
Copy file name to clipboardexpand all lines: src/site/antora/modules/ROOT/pages/manual/logbuilder.adoc
+11-6
Original file line number
Diff line number
Diff line change
@@ -53,7 +53,7 @@ Next to the traditional `info()`, `error()`, etc. `Logger` methods, Log4j API al
53
53
54
54
Developers use Log4j traditionally with logging statements like:
55
55
56
-
[source,java]
56
+
[source,java]
57
57
----
58
58
LOGGER.error("Unable to process request due to {}", errorCode, exception);
59
59
----
@@ -66,13 +66,14 @@ This style has certain drawbacks:
66
66
The fluent interface (also referred to as _the fluent API_) has been added to Log4j API to increase code legibility and avoid ambiguities.
67
67
For instance, the above `error()` call can be expressed using the fluent API as follows:
68
68
69
-
[source,java]
69
+
[source,java]
70
70
----
71
71
LOGGER
72
72
.atError() // <1>
73
73
.withThrowable(exception) // <2>
74
74
.log("Unable to process request due to {}", errorCode); // <3>
75
75
----
76
+
76
77
<1> The log level is set to `ERROR`
77
78
<2> The associated exception is attached
78
79
<3> The log message is formatted with the `errorCode` parameter
@@ -82,7 +83,9 @@ With this syntax, it is clear that the `exception` is part of the log event and
82
83
[#usage]
83
84
== Usage
84
85
85
-
The fluent API entry point is link:../log4j-api/apidocs/org/apache/logging/log4j/LogBuilder.html[`LogBuilder`], which can be obtained by using one of the following `Logger` methods:
which can be obtained by using one of the following `Logger` methods:
86
89
87
90
- `atTrace()`
88
91
- `atDebug()`
@@ -97,20 +100,21 @@ The fluent API entry point is link:../log4j-api/apidocs/org/apache/logging/log4j
97
100
98
101
- `withMarker()`
99
102
- `withThrowable()`
100
-
- `withLocation()`
103
+
- `withLocation()`
101
104
102
105
After that, developers can call the `log()` method to finalize and send the log event.
103
106
104
107
In the following example, we log a parameterized message at `INFO` level, and attach a marker and an exception to the log event:
105
108
106
-
[source,java]
109
+
[source,java]
107
110
----
108
111
LOGGER
109
112
.atInfo() // <1>
110
113
.withMarker(marker) // <2>
111
114
.withThrowable(exception) // <3>
112
115
.log("Unable to process request due to {}", errorCode); // <4>
113
116
----
117
+
114
118
<1> The log level is set to `INFO`
115
119
<2> `marker` is attached to the log event
116
120
<3> `exception` is attached to the log event
@@ -121,13 +125,14 @@ LOGGER
121
125
122
126
The fluent API allows users to instruct the location information to be *eagerly* populated in the log event using the `withLocation()` method:
123
127
124
-
[source,java]
128
+
[source,java]
125
129
----
126
130
LOGGER
127
131
.atInfo()
128
132
.withLocation() // <1>
129
133
.log("Login for user with ID `{}` failed", userId);
130
134
----
135
+
131
136
<1> Instructing to eagerly populate the location information
132
137
133
138
Capturing location information using `withLocation()` is orders of magnitude more efficient compared to letting the `Logger` to figure it out indirectly.
Copy file name to clipboardexpand all lines: src/site/antora/modules/ROOT/pages/manual/thread-context.adoc
+15-4
Original file line number
Diff line number
Diff line change
@@ -23,7 +23,8 @@ Thread Context is one of many xref:manual/api.adoc#fish-tagging[_fish tagging_ c
23
23
[#usage]
24
24
== Usage
25
25
26
-
The entry point for associating logging-related information with the executing thread is link:../log4j-api/apidocs/org/apache/logging/log4j/ThreadContext.html[`ThreadContext`].
26
+
The entry point for associating logging-related information with the executing thread is
* map-structured – referred to as _Thread Context Map_ or _Mapped Diagnostic Context (MDC)_
@@ -48,6 +49,7 @@ void performWork() {
48
49
49
50
ThreadContext.clear(); // <5>
50
51
----
52
+
51
53
<1> Adding properties to the thread context map
52
54
<2> Pushing properties to the thread context stack
53
55
<3> Added properties can later on be used to, for instance, filter the log event, provide extra information in the layout, etc.
@@ -58,7 +60,11 @@ ThreadContext.clear(); // <5>
58
60
=== Auto-clearing thread context
59
61
60
62
When placing items on the thread context stack or map, it's necessary to remove them again when appropriate.
61
-
To assist with this, you can use link:../log4j-api/apidocs/org/apache/logging/log4j/CloseableThreadContext.html[`CloseableThreadContext`] (implementing http://docs.oracle.com/javase/7/docs/api/java/lang/AutoCloseable.html[`AutoCloseable`]) in a try-with-resources block:
Custom thread context map implementations can be provided by setting xref:#log4j2.threadContextMap[the `log4j2.threadContextMap` system property] to the fully-qualified class name of the custom implementation class extending from link:../javadoc/log4j-core/org/apache/logging/log4j/spi/ThreadContextMap.html[`ThreadContextMap`].
147
+
Custom thread context map implementations can be provided by setting xref:#log4j2.threadContextMap[the `log4j2.threadContextMap` system property] to the fully-qualified class name of the custom implementation class extending from
While providing a custom thread context map implementation, you are advised to also extend from link:../javadoc/log4j-core/org/apache/logging/log4j/spi/ReadOnlyThreadContextMap.html[`ReadOnlyThreadContextMap`] too.
150
+
While providing a custom thread context map implementation, you are advised to also extend from
Copy file name to clipboardexpand all lines: src/site/antora/modules/ROOT/pages/manual/webapp.adoc
+2-1
Original file line number
Diff line number
Diff line change
@@ -64,7 +64,7 @@ For performance reasons, containers often ignore certain JARs known not to conta
64
64
65
65
==== The Long Story
66
66
67
-
The Log4j 2 Web JAR file is a web-fragment configured to order before any other web fragments in your application. It contains a `ServletContainerInitializer` (`Log4jServletContainerInitializer`) that the container automatically discovers and initializes. This adds the link:../javadoc/log4j-core/org/apache/logging/log4j/web/Log4jServletContextListener.html[Log4jServletContextListener] and `Log4jServletFilter` to the `ServletContext`. These classes properly initialize and deinitialize the Log4j configuration.
67
+
The Log4j 2 Web JAR file is a web-fragment configured to order before any other web fragments in your application. It contains a `ServletContainerInitializer` (`Log4jServletContainerInitializer`) that the container automatically discovers and initializes. This adds the `Log4jServletContextListener` and `Log4jServletFilter` to the `ServletContext`. These classes properly initialize and deinitialize the Log4j configuration.
68
68
69
69
For some users, automatically starting Log4j is problematic or undesirable. You can easily disable this feature using the `isLog4jAutoInitializationDisabled` context parameter. Simply add it to your deployment descriptor with the value "true" to disable auto-initialization. You _must_ define the context parameter in `web.xml`. If you set in programmatically, it will be too late for Log4j to detect the setting.
70
70
@@ -169,6 +169,7 @@ For security reasons, from Log4j 2.17.0, JNDI must be enabled by setting system
169
169
You may want to use information about the web application during configuration. For example, you could embed the web application's context path in the name of a Rolling File Appender. See WebLookup in Lookups for more information.
170
170
171
171
=== JavaServer Pages Logging
172
+
172
173
You may use Log4j 2 within JSPs just as you would within any other Java code. Simply obtain a Logger and call its methods to log events. However, this requires you to use Java code within your JSPs, and some development teams rightly are not comfortable doing this. If you have a dedicated user interface development team that is not familiar with using Java, you may even have Java code disabled in your JSPs.
173
174
174
175
For this reason, Log4j 2 provides a JSP Tag Library that enables you to log events without using any Java code. To read more about using this tag library, xref:log4j-taglib.adoc[read the Log4j Tag Library documentation].
Copy file name to clipboardexpand all lines: src/site/antora/modules/ROOT/partials/manual/systemproperties/properties-garbage-collection.adoc
+6-2
Original file line number
Diff line number
Diff line change
@@ -38,7 +38,9 @@ This prevents allocating temporary `String` and `char[]` instances.
38
38
| Default value | `8192`
39
39
|===
40
40
41
-
The size in bytes of the link:../https://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html[ByteBuffer]s stored in `ThreadLocal` fields by layouts and
0 commit comments