Skip to content

Getting Exceptions in log4j json template layout when using pointMatcherStrings in stacktrace #2913

@Anshul0507

Description

@Anshul0507

Description

Currently, my application produces logs in text format and I need to convert them to json format. The issue with the raw format was that we were getting long exception trails. So our objective was

  1. Make sure logs are converted to json
  2. We must have stacktrace in the error logs, but the stack trace should be of our application only, java internal stacktrace should not come in logs
  3. Any other stacktraces or exceptions should not come in logs. i.e., we must only have json logs separated by \n

Configuration of errorStackTrace in log4j2-template.json
{ "$resolver": "exception", "field": "stackTrace", "stackTrace": { "stringified": { "truncation": { "suffix" : "... [truncated]", "pointMatcherStrings": ["java.base"] } } } }

After using this configuration, I am getting error stacktrace from log4j in my application logs -

2024-09-02 01:23:47,390 http-nio-8080-exec-2 ERROR An exception occurred processing Appender LogToRollingFile java.lang.ArrayIndexOutOfBoundsException: Index 1500 out of bounds for length 1500

Configuration

Version: [2.19.1]

Operating system: [mac OS 14]

JDK: [JDK 17]

Logs

2024-09-02 00:23:47,390 http-nio-8080-exec-2 ERROR An exception occurred processing Appender LogToRollingFile java.lang.ArrayIndexOutOfBoundsException: Index 1500 out of bounds for length 1500
	at org.apache.logging.log4j.layout.template.json.util.TruncatingBufferedWriter.charAt(TruncatingBufferedWriter.java:214)
	at org.apache.logging.log4j.layout.template.json.util.TruncatingBufferedPrintWriter.charAt(TruncatingBufferedPrintWriter.java:69)
	at org.apache.logging.log4j.layout.template.json.resolver.StackTraceStringResolver.findLineStartIndex(StackTraceStringResolver.java:283)
	at org.apache.logging.log4j.layout.template.json.resolver.StackTraceStringResolver.findLabeledLineStartIndex(StackTraceStringResolver.java:226)
	at org.apache.logging.log4j.layout.template.json.resolver.StackTraceStringResolver.truncate(StackTraceStringResolver.java:133)
	at org.apache.logging.log4j.layout.template.json.resolver.StackTraceStringResolver.truncate(StackTraceStringResolver.java:113)
	at org.apache.logging.log4j.layout.template.json.resolver.StackTraceStringResolver.resolve(StackTraceStringResolver.java:91)
	at org.apache.logging.log4j.layout.template.json.resolver.ExceptionResolver.lambda$createStackTraceStringResolver$3(ExceptionResolver.java:307)

Reproduction

We can reproduce it by causing any null pointer exception in a spring boot application.
I don't want anything else in my application logs apart from json bodies. But if I want to use the pointMatcherStrings feature, then I am getting log4j exception stacktrace

Metadata

Metadata

Assignees

Labels

layoutsAffects one or more Layout plugins

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions