Skip to content

Commit 280cb45

Browse files
committed
change log level at run time without restarting server
0 parents  commit 280cb45

File tree

9 files changed

+731
-0
lines changed

9 files changed

+731
-0
lines changed

Diff for: application.log

+553
Large diffs are not rendered by default.

Diff for: pom.xml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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>2.4.0</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.example</groupId>
12+
<artifactId>spring-boot-logging-example</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>spring-boot-logging-example</name>
15+
<description>Demo project for Spring Boot</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>ch.qos.logback</groupId>
24+
<artifactId>logback-classic</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-web</artifactId>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-test</artifactId>
34+
<scope>test</scope>
35+
</dependency>
36+
</dependencies>
37+
38+
<build>
39+
<plugins>
40+
<plugin>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-maven-plugin</artifactId>
43+
</plugin>
44+
</plugins>
45+
</build>
46+
47+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.springbootloggingexample;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringBootLoggingExampleApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringBootLoggingExampleApplication.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.example.springbootloggingexample.controller;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.RequestParam;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
import com.example.springbootloggingexample.service.LogService;
9+
10+
@RestController
11+
public class LogController {
12+
13+
@Autowired
14+
private LogService logService;
15+
16+
@GetMapping("/changeloglevel")
17+
public String changeLogLevel(@RequestParam(required = false) String loggerName,
18+
@RequestParam(required = false) String level) {
19+
return logService.setLoglevel(loggerName, level);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.example.springbootloggingexample.controller;
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 MessageController {
10+
11+
Logger logger = LoggerFactory.getLogger(MessageController.class);
12+
13+
@GetMapping("/message")
14+
public String getMessage() {
15+
logger.info("[getMessage] info message");
16+
logger.warn("[getMessage] warn message");
17+
logger.error("[getMessage] error message");
18+
logger.debug("[getMessage] debug message");
19+
logger.trace("[getMessage] trace message");
20+
return "open console to check log messages.";
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.example.springbootloggingexample.service;
2+
3+
public interface LogService {
4+
5+
String setLoglevel(String loggerName, String level);
6+
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.example.springbootloggingexample.service.impl;
2+
3+
import org.slf4j.LoggerFactory;
4+
import org.springframework.stereotype.Service;
5+
6+
import com.example.springbootloggingexample.service.LogService;
7+
8+
@Service
9+
public class LogServiceImpl implements LogService{
10+
11+
@Override
12+
public String setLoglevel(String loggerName, String level) {
13+
ch.qos.logback.classic.Logger root = getLogger(loggerName);
14+
return setLogLevel(level, root);
15+
}
16+
17+
private ch.qos.logback.classic.Logger getLogger(String loggerName) {
18+
ch.qos.logback.classic.Logger root;
19+
if (loggerName != null && !loggerName.isEmpty())
20+
root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(loggerName);
21+
else
22+
root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
23+
return root;
24+
}
25+
26+
private String setLogLevel(String level, ch.qos.logback.classic.Logger root) {
27+
if ("info".equalsIgnoreCase(level)) {
28+
root.setLevel(ch.qos.logback.classic.Level.INFO);
29+
} else if ("debug".equalsIgnoreCase(level)) {
30+
root.setLevel(ch.qos.logback.classic.Level.DEBUG);
31+
} else if ("warn".equalsIgnoreCase(level)) {
32+
root.setLevel(ch.qos.logback.classic.Level.WARN);
33+
} else if ("trace".equalsIgnoreCase(level)) {
34+
root.setLevel(ch.qos.logback.classic.Level.TRACE);
35+
} else if ("error".equalsIgnoreCase(level)) {
36+
root.setLevel(ch.qos.logback.classic.Level.ERROR);
37+
} else if ("off".equalsIgnoreCase(level)) {
38+
root.setLevel(ch.qos.logback.classic.Level.OFF);
39+
} else {
40+
root.setLevel(ch.qos.logback.classic.Level.ALL);
41+
level = "all";
42+
}
43+
return "logger level set as " + level;
44+
}
45+
}

Diff for: src/main/resources/application.properties

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#logging.level.com.example.springbootloggingexample.controller=DEBUG
2+
3+
# Logging pattern for the console
4+
logging.pattern.console= %date{ISO8601} %-5level %class{0}:%L - [%X] %msg%n
5+
6+
#logging pattern for file
7+
logging.pattern.file=%date{ISO8601} %-5level %class{0}:%L - [%X] %msg%n
8+
9+
# output file
10+
logging.file.name=application.log
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.springbootloggingexample;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.boot.test.context.SpringBootTest;
5+
6+
@SpringBootTest
7+
class SpringBootLoggingExampleApplicationTests {
8+
9+
@Test
10+
void contextLoads() {
11+
}
12+
13+
}

0 commit comments

Comments
 (0)