Skip to content

Commit d613928

Browse files
committed
Add note about lazy lookups in PatternLayout
1 parent b63461d commit d613928

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

src/site/antora/modules/ROOT/pages/manual/lookups.adoc

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,11 @@ plugins, each one associated with a prefix, which retrieve data from external so
7272
`StrLookup` is a simple map-like interface.
7373
The main difference between a map and `StrLookup` is that the latter can compute the value of a key programmatically in a global context or in the context of log event.
7474
75+
[#common-concerns]
76+
== Common concerns
77+
7578
[#evaluation-contexts]
76-
== Evaluation contexts
79+
=== Evaluation contexts
7780
7881
Each lookup has an associated prefix, and Log4j can evaluate it in one of the following ways:
7982
@@ -108,6 +111,23 @@ https://docs.oracle.com/javase/{java-target-version}/docs/api/java/lang/System.h
108111
* When used in the context of an event, it formats the **event** timestamp obtained through
109112
link:../javadoc/log4j-core/org/apache/logging/log4j/core/LogEvent.html#getTimeMillis()[`LogEvent.getTimeMillis()`].
110113
114+
[#lookups-patterns]
115+
=== Lazy lookups and pattern converters
116+
117+
For historical reasons, the
118+
xref:manual/pattern-layout.adoc#plugin-attr-pattern[`pattern` attribute of PatternLayout]
119+
supports two similar string replacement mechanisms:
120+
121+
* `+${...}+` property expressions.
122+
* xref:manual/pattern-layout.adoc#converters[`%<name>` pattern converters].
123+
124+
Both lazy `+$${...}+` property expressions and pattern converters have access to the value of the current `LogEvent` and can provide similar results.
125+
There is, however, an important difference between them:
126+
127+
* Pattern converters can be garbage-free.
128+
See xref:manual/pattern-layout.adoc#garbage-free[Garbage-free pattern converters] for more details.
129+
* Lazy lookups are **not** garbage-free and always create temporary `String` objects.
130+
111131
[#collection]
112132
== Collection
113133
@@ -295,6 +315,8 @@ xref:manual/thread-context.adoc#custom-ContextDataProvider[Custom context data p
295315
====
296316
Don't use `$$+{ctx:key}+` in the xref:manual/pattern-layout.adoc[] conversion patterns!
297317
Use xref:manual/pattern-layout.adoc#converter-thread-context-map[the `%X\{key}` pattern converter] instead.
318+
319+
See <<lookups-patterns>> for more information.
298320
====
299321
300322
[#DateLookup]
@@ -326,6 +348,8 @@ When used in the context of a log event, the timestamp of the log event is used.
326348
====
327349
Don't use `$$+{date:format}+` in the xref:manual/pattern-layout.adoc[] conversion patterns!
328350
Use xref:manual/pattern-layout.adoc#converter-date[the `%d\{key}` pattern converter] instead.
351+
352+
See <<lookups-patterns>> for more information.
329353
====
330354
331355
[#DockerLookup]
@@ -424,6 +448,14 @@ It supports the retrieval of the following event attributes:
424448
|UNIX timestamp in milliseconds of the log event
425449
|===
426450
451+
[TIP]
452+
====
453+
Don't use `$$+{event:key}+` in the xref:manual/pattern-layout.adoc[] conversion patterns!
454+
There is a xref:manual/pattern-layout.adoc#converters[specialized pattern converter replacement] for each of these lookups.
455+
456+
See <<lookups-patterns>> for more information.
457+
====
458+
427459
[#JavaLookup]
428460
=== Java Lookup
429461
@@ -662,6 +694,8 @@ xref:manual/messages.adoc#MapMessage[`MapMessage`].
662694
====
663695
Don't use `$$+{map:key}+` in the xref:manual/pattern-layout.adoc[] conversion patterns!
664696
Use xref:manual/pattern-layout.adoc#converter-map[the `%K\{key}` pattern converter] instead.
697+
698+
See <<lookups-patterns>> for more information.
665699
====
666700
667701
[#MarkerLookup]
@@ -697,6 +731,8 @@ link:../javadoc/log4j-core/org/apache/logging/log4j/core/LogEvent.html[log event
697731
====
698732
Don't use `$$+{marker:}+` in the xref:manual/pattern-layout.adoc[] conversion patterns!
699733
Use xref:manual/pattern-layout.adoc#converter-marker[the `%markerSimpleName` pattern converter] instead.
734+
735+
See <<lookups-patterns>> for more information.
700736
====
701737
702738
[#SpringBootLookup]
@@ -800,8 +836,10 @@ of a `StructuredDataMessage`.
800836
801837
[TIP]
802838
====
803-
Except `++$${sd:id}++` and `++$${sd:type}++`, don't use `++$${sd:key}++` in the xref:manual/pattern-layout.adoc[] conversion patterns!
839+
Except `++$${sd:id}++` and `++$${sd:type}++`, don't use other `++$${sd:key}++` expressions in the xref:manual/pattern-layout.adoc[] conversion patterns!
804840
Use xref:manual/pattern-layout.adoc#converter-map[the `%K\{key}` pattern converter] instead.
841+
842+
See <<lookups-patterns>> for more information.
805843
====
806844
807845
[#SystemPropertiesLookup]
@@ -983,7 +1021,7 @@ While annotating your lookup with `@Plugin`, you need to make sure that
9831021
* It has a unique `name` attribute across all available `StrLookup` plugins
9841022
* The `category` attribute is set to link:../javadoc/log4j-core/org/apache/logging/log4j/core/lookup/StrLookup.html#CATEGORY[`StrLookup.CATEGORY`]
9851023
986-
You can check out following files for examples:
1024+
You can check out the following files for examples:
9871025
9881026
* {project-github-url}/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/DateLookup.java[`LowerLookup.java`] – <<LowerLookup>> lower-cases its input
9891027
* {project-github-url}/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/EventLookup.java[`EventLookup.java`] – <<EventLookup>> extracts specified fields from the effective `LogEvent` in the context

0 commit comments

Comments
 (0)