logback-logstash-syntax-highlighting-decorators
ANSI syntax highlighting for logstash-logback-encoder JSON output.
Features:
- Syntax highlighting (and pretty printing)
- Special field handling
- Level: trace, debug, info, warn and error
- Message
The primary use-case for this tool is coloring JSON console-output during local development / unit testing.
In production rather configure an encoder without a Decorator
and apply proper visualization tools like Kibana.
Use conditional processing to differentiate between different environments in Logback configuration.
Example output
License
Obtain
The project is based on Maven and is available at central Maven repository.
Maven coordinates
Add the property
<logback-logstash-syntax-highlighting-decorators.version>1.0.6</logback-logstash-syntax-highlighting-decorators.version>
then add
<dependency>
<groupId>com.github.skjolber.logback-logstash-syntax-highlighting-decorators</groupId>
<artifactId>logback-logstash-syntax-highlighting-decorators</artifactId>
<version>${logback-logstash-syntax-highlighting-decorators.version}</version>
</dependency>
or
Gradle coordinates
For
ext {
logbackLogstashSyntaxHighlightingDecoratorsVersion = '1.0.6'
}
add
implementation ("com.github.skjolber.logback-logstash-syntax-highlighting-decorators:logback-logstash-syntax-highlighting-decorators:${logbackLogstashSyntaxHighlightingDecoratorsVersion}")
Usage
Add a JsonGeneratorDecorator:
<appender name="STDOUT_JSON" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<!-- add pretty-printing and syntax highlighting for testing -->
<jsonGeneratorDecorator class="com.github.skjolber.decorators.SyntaxHighlightingDecorator"/>
</encoder>
</appender>
The default decorator is aware of the log-level and highlights WARN
and ERROR
with yellow and red background colors.
Custom colors
Define your own colors using ConfigurableSyntaxHighlighter
:
<jsonGeneratorDecorator class="com.github.skjolber.decorators.SyntaxHighlightingDecorator">
<syntaxHighlighterFactory class="com.github.skjolber.decorators.factory.ConfigurableSyntaxHighlighterFactory">
<stringValue>blue</stringValue>
<numberValue>black highIntensity</numberValue>
<fieldName>red</fieldName>
<binaryValue>yellowBackground</binaryValue>
<booleanValue>cyan</booleanValue>
<nullValue>black</nullValue>
<curlyBrackets>green</curlyBrackets>
<squareBrackets>green</squareBrackets>
<colon>green</colon>
<whitespace>green</whitespace>
<comma>green</comma>
</syntaxHighlighterFactory>
</jsonGeneratorDecorator>
and space-separated foreground, background and style keys. For special handling of fields message
and level
, use the LogLevelSyntaxHighlighterFactory
:
<jsonGeneratorDecorator class="com.github.skjolber.decorators.SyntaxHighlightingDecorator">
<syntaxHighlighterFactory class="com.github.skjolber.decorators.factory.LogLevelSyntaxHighlighterFactory">
<level>
<info>green</info>
<warning>yellow</warning>
<error>red</error>
</level>
<message>highIntensity blue</message>
</syntaxHighlighterFactory>
</jsonGeneratorDecorator>
Pretty-printing
Pretty-printing (with newline + indent) is enabled by default. To disable it, a prettyPrint
-element under SyntaxHighlightingDecorator
like so:
<jsonGeneratorDecorator class="com.github.skjolber.decorators.SyntaxHighlightingDecorator">
<prettyPrint>false</prettyPrint>
</jsonGeneratorDecorator>
Foreground color
Key | Text color |
---|---|
default | Default text |
black | Black text |
red | Red text |
green | Green text |
yellow | Yellow text |
blue | Blue text |
magenta | Magneta text |
cyan | Cyan text |
white | White text |
Background color
Key | Background color |
---|---|
defaultBackground | Default |
blackBackground | Black |
redBackground | Red |
greenBackground | Green |
yellowBackground | Yellow |
blueBackground | Blue |
magentaBackground | Magneta |
cyanBackground | Cyan |
whiteBackground | White |
Style
Key | Style |
---|---|
highIntensity | High intensity (bold) |
lowIntensity | Low instensity |
italic | Italic |
underline | Underline |
blink | Blink |
Details
The SyntaxHighlightingDecorator
supports a list of <syntaxHighlighterFactory>
elements. The colors are applied in chronological order. Highlighters not wanting to contribute to the current JSON event are expected to return an ANSI reset (including escaping).
Tips and tricks
Exclude fields with low information value during testing. For example:
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<!-- remove unnecessary fields in testing -->
<fieldNames>
<levelValue>[ignore]</levelValue>
<version>[ignore]</version>
</fieldNames>
</encoder>
History
- 1.0.6: Do not set default colors.
- 1.0.5: Add option for single-line output
- 1.0.1-1.0.4: Update Jackson dependency due to security issue and dependency updates
- 1.0.0: Initial version