When passing data to LogStash, it will show escape character in log message. But sometime, it cannot update logstash pipeline to replace specific character so need to implement in code level.
This problem caused by escape character found in String. As LogstashEncoder will serialize object to JSON, it will replace escape ASCII character to encoded value (e.g. new line => \n).
To resolve it, it can use CharacterEscapesJsonFactoryDecorator in log settings file (e.g. log4j.xml) to override specific ASCII code and replaced by defined value.
This example will replace string to empty string.
<appender name="console-json" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="net.logstash.logback.encoder.LogstashEncoder" > <jsonFactoryDecorator class="net.logstash.logback.decorate.CharacterEscapesJsonFactoryDecorator"> <escape> <!-- ASCII 10 => new line --> <targetCharacterCode>10</targetCharacterCode> <escapeSequence></escapeSequence> </escape> </jsonFactoryDecorator> </encoder> </appender>
Test result:
Leave a Reply