newrelic / newrelic-java-agent

The New Relic Java agent
Apache License 2.0
192 stars 140 forks source link

Add local decorating for customers using Log4j 2 with JsonTemplateLayout #1865

Closed obenkenobi closed 3 weeks ago

obenkenobi commented 3 weeks ago

Overview

Adds local decorating (i.e. NR-LINKING metadata) to log4j 2 logs using JsonTemplateLayout such as shown configured in the XML snippet below:

    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <!-- THIS DOES NOT WORK! Use JsonTemplateLayout with default JsonLayout provided by log4j2 -->
                         <JsonTemplateLayout eventTemplateUri="classpath:JsonLayout.json"/>
        </Console>
    </Appenders>

Example log without local decorating:

{
  "@timestamp": "2024-04-18T21:08:01.250Z",
  "log.level": "INFO",
  "process.thread.name": "http-nio-8081-exec-1",
  "log.logger": "org.springframework.samples.petclinic.system.WelcomeController",
  "message": "info bar"
}

Example log with local decorating:

{
  "@timestamp": "2024-04-18T21:08:01.250Z",
  "log.level": "INFO",
  "process.thread.name": "http-nio-8081-exec-1",
  "log.logger": "org.springframework.samples.petclinic.system.WelcomeController",
  "message": "info bar NR-LINKING|MzQ3Nzg3NHxBUE18QVBQTElDQVRJT058NTM4Mzc3ODY0|192.168.0.31|e0a726b1de7a78af9e74bfb042845180|3f4885dca4f83070|Petclinic+Log4j+Jsontemplatelayout+decorating|"
}

Related Github Issue

#1730

Testing

The agent includes a suite of tests which should be used to verify your changes don't break existing functionality. These tests will run with Github Actions when a pull request is made. More details on running the tests locally can be found here,

Checks