Skip to content

Instantly share code, notes, and snippets.

@AlecIsaacson
Last active February 12, 2020 18:30
Show Gist options
  • Save AlecIsaacson/34fde18e6036ecaf8191b98e68655fd1 to your computer and use it in GitHub Desktop.
Save AlecIsaacson/34fde18e6036ecaf8191b98e68655fd1 to your computer and use it in GitHub Desktop.
Sample Build.Gradle that adds the two lines to enable New Relic Logs in Context for Log4J v2.x.
//
// This is a full example showing the two lines you need to add to your Gradle file to enable New Relic Logs in Context.
// Most of this file is stock - the only changes needed shown below, flagged with comments.
//
description = 'Ad Service'
buildscript {
repositories {
mavenCentral()
mavenLocal()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.3'
classpath "gradle.plugin.com.github.sherter.google-java-format:google-java-format-gradle-plugin:0.7.1"
}
}
apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'com.google.protobuf'
apply plugin: 'com.github.sherter.google-java-format'
repositories {
mavenCentral()
mavenLocal()
}
configurations {
newrelic
}
group = "adservice"
version = "0.1.0-SNAPSHOT"
def opencensusVersion = "0.18.0"
def grpcVersion = "1.17.0"
def jacksonVersion = "2.9.6"
tasks.withType(JavaCompile) {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
}
ext {
speed = project.hasProperty('speed') ? project.getProperty('speed') : false
offlineCompile = new File("$buildDir/output/lib")
newrelicVersion = '5.8.0'
}
dependencies {
newrelic "com.newrelic.agent.java:newrelic-agent:${newrelicVersion}"
//New Relic Logs in Context - Add this line for Log4J support.
compile("com.newrelic.logging:log4j2:1.0-rc2")
if (speed) {
compile fileTree(dir: offlineCompile, include: '*.jar')
} else {
compile "com.google.api.grpc:proto-google-common-protos:1.12.0",
"io.opencensus:opencensus-api:${opencensusVersion}",
"io.opencensus:opencensus-contrib-grpc-util:${opencensusVersion}",
"io.opencensus:opencensus-exporter-trace-jaeger:${opencensusVersion}",
"io.opencensus:opencensus-exporter-stats-stackdriver:${opencensusVersion}",
"io.opencensus:opencensus-exporter-trace-stackdriver:${opencensusVersion}",
"io.opencensus:opencensus-exporter-trace-logging:${opencensusVersion}",
"io.grpc:grpc-protobuf:${grpcVersion}",
"io.grpc:grpc-stub:${grpcVersion}",
"io.grpc:grpc-netty:${grpcVersion}",
"io.grpc:grpc-services:${grpcVersion}",
"org.apache.logging.log4j:log4j-core:2.11.1"
runtime "com.fasterxml.jackson.core:jackson-core:${jacksonVersion}",
"com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}",
"io.opencensus:opencensus-contrib-log-correlation-log4j2:${opencensusVersion}",
"io.opencensus:opencensus-impl:${opencensusVersion}",
"io.netty:netty-tcnative-boringssl-static:2.0.8.Final"
}
}
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.5.1-1'
}
plugins {
grpc {
artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}"
}
}
generateProtoTasks {
all()*.plugins {
grpc {}
}
ofSourceSet('main')
}
}
googleJavaFormat {
toolVersion '1.7'
}
// Inform IDEs like IntelliJ IDEA, Eclipse or NetBeans about the generated code.
sourceSets {
main {
java {
srcDirs 'hipstershop'
srcDirs 'build/generated/source/proto/main/java/hipstershop'
srcDirs 'build/generated/source/proto/main/grpc/hipstershop'
}
}
}
// Provide convenience executables for trying out the examples.
apply plugin: 'application'
startScripts.enabled = false
// This to cache dependencies during Docker image building. First build will take time.
// Subsequent build will be incremental.
task downloadRepos(type: Copy) {
from configurations.compile
into offlineCompile
from configurations.runtime
into offlineCompile
}
task adService(type: CreateStartScripts) {
mainClassName = 'hipstershop.AdService'
applicationName = 'AdService'
outputDir = new File(project.buildDir, 'tmp')
classpath = jar.outputs.files + project.configurations.runtime
// New Relic Logs in Context - set log4j2.messageFactory.
defaultJvmOpts =
["-javaagent:/app/build/libs/newrelic.jar","-Dnewrelic.config.file=/app/newrelic/newrelic.yml","-Dnewrelic.config.app_name=adservice-server","-Dlog4j2.messageFactory=com.newrelic.logging.log4j2.NewRelicMessageFactory"]
}
task adServiceClient(type: CreateStartScripts) {
mainClassName = 'hipstershop.AdServiceClient'
applicationName = 'AdServiceClient'
outputDir = new File(project.buildDir, 'tmp')
classpath = jar.outputs.files + project.configurations.runtime
// New Relic Logs in Context - set log4j2.messageFactory.
defaultJvmOpts =
["-javaagent:/app/build/libs/newrelic.jar","-Dnewrelic.config.file=/app/newrelic/newrelic.yml","-Dnewrelic.config.app_name=adservice-client","-Dlog4j2.messageFactory=com.newrelic.logging.log4j2.NewRelicMessageFactory"]
}
task copyAgent(type: Copy) {
from { configurations.newrelic } into "$buildDir/libs"
from { "newrelic/newrelic.yml "} into "$buildDir/libs"
rename ("newrelic-agent-${newrelicVersion}.jar", 'newrelic.jar')
}
applicationDistribution.into('bin') {
from(adService)
from(adServiceClient)
fileMode = 0755
}
adService.dependsOn copyAgent
adServiceClient.dependsOn copyAgent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment