Skip to content

Instantly share code, notes, and snippets.

@vy
Created March 26, 2019 08:28
Show Gist options
  • Save vy/81f6fa196f4a1ec453e4f29d33012fb3 to your computer and use it in GitHub Desktop.
Save vy/81f6fa196f4a1ec453e4f29d33012fb3 to your computer and use it in GitHub Desktop.
How to pass a Spring property to Log4j in a Spring Boot application
package com.vlkan.demo.spring;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.annotation.PostConstruct;
@SpringBootApplication
public class DemoSpringAppApplication {
private static final Logger LOGGER = LoggerFactory.getLogger(DemoSpringAppApplication.class);
@Autowired
public DemoSpringAppApplication(@Value("${spring.profiles.active}") String profile) {
System.setProperty("spring-profile", profile);
}
@PostConstruct
public void startUp() {
LOGGER.warn("started");
}
public static void main(String[] args) {
SpringApplication.run(DemoSpringAppApplication.class, args);
}
}
{
"a_profile": "${sys:a_profile}",
"b_profile": "${b_profile}",
"mdc": "${json:mdc}",
"exception": {
"exception_class": "${json:exception:className}",
"exception_message": "${json:exception:message}",
"stacktrace": "${json:exception:stackTrace:text}"
},
"line_number": "${json:source:lineNumber}",
"class": "${json:source:className}",
"@version": 1,
"source_host": "${hostName}",
"message": "${json:message}",
"thread_name": "${json:thread:name}",
"@timestamp": "${json:timestamp}",
"level": "${json:level}",
"file": "${json:source:fileName}",
"method": "${json:source:methodName}",
"logger_name": "${json:logger:name}"
}
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Properties>
<property name="a_profile">${sys:spring.profiles.active}</property>
<property name="b_profile">${bundle:application:spring.profiles.active}</property>
</Properties>
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<LogstashLayout dateTimeFormatPattern="yyyy-MM-dd'T'HH:mm:ss.SSSZZZ"
eventTemplateUri="classpath:log4j2-layout.json"
prettyPrintEnabled="true"
stackTraceEnabled="true"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="CONSOLE"/>
</Root>
</Loggers>
</Configuration>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.vlkan.demo.spring</groupId>
<artifactId>demo-spring-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>com.vlkan.log4j2</groupId>
<artifactId>log4j2-logstash-layout</artifactId>
<version>0.16</version>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment