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
Note that how `Hello, {}!` should be encoded given the `\{name}` array as argument completely depends on the link:../javadoc/log4j-api/org/apache/logging/log4j/message/MessageFactory.html[`MessageFactory`] employed.
146
136
Log4j allows users to customize this behaviour in several `getLogger()` methods of link:../javadoc/log4j-api/org/apache/logging/log4j/LogManager.html[`LogManager`]:
<1> Create a logger using the default message factory
159
144
<2> Use default parameter placeholders, that is, `{}` style
160
145
<3> Explicitly provide the message factory, that is, link:../javadoc/log4j-api/org/apache/logging/log4j/message/StringFormatterMessageFactory.html[`StringFormatterMessageFactory`].
@@ -173,11 +158,7 @@ If you need more control over how the parameters are formatted, you can also use
173
158
174
159
[source,java]
175
160
----
176
-
Logger logger = LogManager.getFormatterLogger();
177
-
logger.debug("Logging in user %s with birthday %s", user.getName(), user.getBirthdayCalendar());
178
-
logger.debug("Logging in user %1$s with birthday %2$tm %2$te,%2$tY", user.getName(), user.getBirthdayCalendar());
Keep in mind that, contrary to the formatter logger, the default Log4j logger (i.e., `{}`-style parameters) is heavily optimized for several use cases and can operate xref:manual/garbagefree.adoc[garbage-free] when configured correctly.
203
182
You might reconsider your formatter logger usages for latency sensitive applications.
204
183
205
-
[#resource-logger]
206
-
=== Resource logger
207
-
208
-
Resource loggers, introduced in Log4j API `2.24.0`, is a special kind of `Logger` that:
209
-
210
-
* is a regular class member variable that will be garbage collected along with the class instance
211
-
* enriches generated log events with data associated with the resource (i.e., the class instance)
212
-
213
-
xref:manual/resource-logger.adoc[Read more on resource loggers...]
214
-
215
184
[#event-logger]
216
185
=== Event logger
217
186
@@ -250,20 +219,15 @@ The fluent API allows you to log using a fluent interface:
xref:manual/logbuilder.adoc[Read more on the Fluent API...]
261
226
262
227
[#fish-tagging]
263
228
== Fish tagging
264
229
265
-
Just as a fish can be tagged and have its movement tracked (aka. _fish tagging_ footnote:[Fish tagging is first described by Neil Harrison in the _"Patterns for Logging Diagnostic Messages"_ chapter of https://dl.acm.org/doi/10.5555/273448[_"Pattern Languages of Program Design 3"_ edited by R. Martin, D. Riehle, and F. Buschmann in 1997].]), stamping log events with a common tag or set of data
266
-
elements allows the complete flow of a transaction or a request to be tracked.
230
+
Just as a fish can be tagged and have its movement tracked (aka. _fish tagging_ footnote:[Fish tagging is first described by Neil Harrison in the _"Patterns for Logging Diagnostic Messages"_ chapter of https://dl.acm.org/doi/10.5555/273448[_"Pattern Languages of Program Design 3"_ edited by R. Martin, D. Riehle, and F. Buschmann in 1997].]), stamping log events with a common tag or set of data elements allows the complete flow of a transaction or a request to be tracked.
267
231
You can use them for several purposes, such as:
268
232
269
233
* Provide extra information while serializing the log event
@@ -287,56 +251,11 @@ Markers are programmatic labels developers can associate to log statements:
287
251
288
252
[source,java]
289
253
----
290
-
public class MyApp {
291
-
292
-
private static final Logger LOGGER = LogManager.getLogger();
293
-
294
-
private static final Marker ACCOUNT_MARKER = MarkerManager.getMarker("ACCOUNT");
295
-
296
-
public void removeUser(String userId) {
297
-
logger.debug(ACCOUNT_MARKER, "Removing user with ID `{}`", userId);
Just like a https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/ScopedValue.html[Java's `ScopedValue`], _Scoped Context_ facilitates associating information with a certain block of code and makings this accessible to the rest of the logging system:
310
-
311
-
[source,java]
312
-
----
313
-
314
-
private class Worker implements Runnable {
315
-
316
-
private static final Logger LOGGER = LogManager.getLogger();
317
-
318
-
public void authUser(Request request, Session session) {
Copy file name to clipboardExpand all lines: src/site/antora/modules/ROOT/partials/log4j-features.adoc
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,7 +32,7 @@ Check out the xref:manual/performance.adoc[Performance] page for details.
32
32
33
33
Extensibility::
34
34
Log4j contains a fully-fledged xref:manual/plugins.adoc[plugin support] that users can leverage to extend functionality.
35
-
You can easily add your components (layouts, appenders, filters, etc.) or customize existing ones (e.g., adding new directives to xref:manual/extending.adoc#PatternConverters[Pattern Layout] or xref:manual/json-template-layout.adoc#extending[JSON Template Layout]).
35
+
You can easily add your components (layouts, appenders, filters, etc.) or customize existing ones (e.g., adding new directives to xref:manual/pattern-layout.adoc#extending[Pattern Layout] or xref:manual/json-template-layout.adoc#extending[JSON Template Layout]).
36
36
Check out the xref:manual/extending.adoc[Extending Log4j] page.
0 commit comments