Skip to content

Commit 0d417c4

Browse files
authored
adding BAEL-6471 custom log level code
1 parent 4a617a5 commit 0d417c4

File tree

6 files changed

+94
-0
lines changed

6 files changed

+94
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>3.5.3</version>
9+
<relativePath/>
10+
</parent>
11+
<artifactId>customloglevel</artifactId>
12+
<name>customloglevel</name>
13+
<description>Demo project for Different Log level</description>
14+
<properties>
15+
<java.version>17</java.version>
16+
</properties>
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-web</artifactId>
21+
</dependency>
22+
</dependencies>
23+
<build>
24+
<plugins>
25+
<plugin>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-maven-plugin</artifactId>
28+
</plugin>
29+
</plugins>
30+
</build>
31+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.customloglevel;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class CustomloglevelApplication {
8+
public static void main(String[] args) {
9+
SpringApplication.run(CustomloglevelApplication.class, args);
10+
}
11+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.customloglevel;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@RestController
9+
public class LogController {
10+
11+
private static final Logger logger = LoggerFactory.getLogger(LogController.class);
12+
13+
@GetMapping("/log")
14+
public String generateLogs() {
15+
logger.trace("This is a TRACE message from controller.");
16+
logger.debug("This is a DEBUG message from controller.");
17+
logger.info("This is an INFO message from controller.");
18+
logger.warn("This is a WARN message from controller.");
19+
logger.error("This is an ERROR message from controller.");
20+
return "Logs generated!";
21+
}
22+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
spring.application.name=customloglevel
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<property name="LOGS_HOME" value="./logs"/>
4+
<property name="CONSOLE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"/>
5+
<property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"/>
6+
<property name="FILE_NAME" value="my-spring-app"/>
7+
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
8+
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
9+
<Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
10+
</encoder>
11+
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
12+
<level>INFO</level>
13+
</filter>
14+
</appender>
15+
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
16+
<file>${LOGS_HOME}/${FILE_NAME}.log</file>
17+
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
18+
<Pattern>${FILE_LOG_PATTERN}</Pattern>
19+
</encoder>
20+
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
21+
<level>DEBUG</level>
22+
</filter>
23+
</appender>
24+
<root level="DEBUG">
25+
<appender-ref ref="CONSOLE"/>
26+
<appender-ref ref="FILE"/>
27+
</root>
28+
</configuration>

logging-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<module>splunk-with-log4j2</module>
2626
<module>jul-to-slf4j</module>
2727
<module>log-all-requests</module>
28+
<module>customloglevel</module>
2829
</modules>
2930

3031
</project>

0 commit comments

Comments
 (0)