Skip to content

Commit 1c27d4e

Browse files
committed
Add a few words in the doc about how to register additional JsonProviders to a LogstashEncoder
1 parent 8c41442 commit 1c27d4e

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

README.md

+32
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ The structure of the output, and the data it contains, is fully configurable.
6262
* [Customizing AccessEvent Message](#customizing-accessevent-message)
6363
* [Customizing Logger Name Length](#customizing-logger-name-length)
6464
* [Customizing Stack Traces](#customizing-stack-traces)
65+
* [Registering Additional Providers](#registering-additional-providers)
6566
* [Prefix/Suffix/Separator](#prefixsuffixseparator)
6667
* [Composite Encoder/Layout](#composite-encoderlayout)
6768
* [Providers common to LoggingEvents and AccessEvents](#providers-common-to-loggingevents-and-accessevents)
@@ -1817,6 +1818,37 @@ For example:
18171818
can even be used within a `PatternLayout` to format stacktraces in any non-JSON logs you may have.
18181819

18191820

1821+
## Registering Additional Providers
1822+
1823+
`LogstashEncoder`, `LogstashAccessEncoder` and their "layout" counterparts all come with a predefined set of encoders. You can register additional JsonProviders using the `<provider>` configuration property as shown in the following example:
1824+
1825+
```xml
1826+
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
1827+
<!-- Add a new provider after those than come with the LogstashEncoder -->
1828+
<provider class="net.logstash.logback.composite.loggingevent.LoggingEventPatternJsonProvider">
1829+
<pattern>
1830+
{
1831+
"message": "%mdc{custom_value} %message"
1832+
}
1833+
</pattern>
1834+
</provider>
1835+
1836+
<!-- Disable the default message provider -->
1837+
<fieldNames>
1838+
<message>[ignore]</message>
1839+
</fieldNames>
1840+
</encoder>
1841+
```
1842+
1843+
You can add several additional JsonProviders using multiple `<provider>` entries. They will appear just after the default providers registered by the LogstashEncoder.
1844+
1845+
In this example, the pattern provider produces a "message" JSON field that will conflict with the message field produced by the MessageJsonProvider already registered by the LogstashEncoder itself. Different options to avoid the conflict:
1846+
1847+
- you instruct LogstashEncoder to use a different field name using the [fieldNames](#customizing-standard-field-names) configuration property;
1848+
- you disable the message provider that comes with the encoder (that's the option illustrated in the example above);
1849+
- you use a different field name in your pattern.
1850+
1851+
18201852
## Prefix/Suffix/Separator
18211853

18221854
You can specify a prefix (written before the JSON object),

0 commit comments

Comments
 (0)