-
-
Save angelmartz/b0d290efba312d6e15f9178f6ca447e4 to your computer and use it in GitHub Desktop.
How to pass a Spring property to Log4j in a Spring Boot application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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}" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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