Lookups
Log4j Core provides a flexible and extensible property substitution system.
The property substitution system is composed of these elements:
-
A string interpolation engine (
StrSubstitutor) that evaluates${...}expressions. These expressions can contain recursive expressions and default values.See property substitution for more details.
-
The
Interpolatorthat evaluates simple${name}expressions.The Interpolator has two functions:
-
If
namedoes not contain a colon:character, the Interpolator uses thePropertiesconfiguration element to resolve its value. -
If
nameis of the formprefix:key, the Interpolator delegates the lookup to aStrLookupassociated withprefixand falls back to evaluating${key}if the lookup was not successful.
-
-
A set of
StrLookupplugins, each one associated with a prefix, which retrieve data from external sources.
StrLookup is a simple map-like interface.
The main difference between a map and StrLookup is that the latter can compute the value of a key dynamically in a global context or in the context of log event.
Common concerns
Evaluation contexts
Each lookup has an associated prefix, and Log4j can evaluate it in one of the following ways:
- Global context
-
In a global context Log4j evaluates
${prefix:key}expressions by callinglookup("key")on the lookup associated toprefix. The result of this call only takes into account the global state of the system.The global context is used to expand the attributes of a configuration file.
- Log event context
-
In the context of a log event
event, Log4j evaluates${prefix:key}expressions by callinglookup(event, "key")on the lookup associated toprefix. The result of this call might take into account the contents of the log event, besides the global state of the system.
Some configuration attributes (e.g.,