Skip to content

Commit d0bbc09

Browse files
Add Endpoint to emit log messages
1 parent faa56fd commit d0bbc09

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

sample-spring-boot/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@
8585
<plugin>
8686
<groupId>org.springframework.boot</groupId>
8787
<artifactId>spring-boot-maven-plugin</artifactId>
88+
<executions>
89+
<execution>
90+
<goals>
91+
<goal>repackage</goal>
92+
</goals>
93+
</execution>
94+
</executions>
8895
</plugin>
8996
</plugins>
9097
</build>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.sap.hcp.cf.logging.sample.springboot.controller;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.web.bind.annotation.PathVariable;
7+
import org.springframework.web.bind.annotation.PostMapping;
8+
import org.springframework.web.bind.annotation.RequestParam;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
/**
12+
* This controller provides an endpoint to generate logs. This can be used to
13+
* test logging.
14+
*/
15+
@RestController
16+
public class LogController {
17+
18+
private static final String DEFAULT_LOG_MESSAGE = "This is the default log message!";
19+
20+
/**
21+
* Generate a log event with the given parameters.
22+
*
23+
* @param loggerName the name of the logger to use
24+
* @param logLevel the level to use, allowed values: error, warn, info, debug,
25+
* trace
26+
* @param message the message to log, defaults to
27+
* {@value #DEFAULT_LOG_MESSAGE}
28+
* @return a response containing the emitted message
29+
*/
30+
@PostMapping("/log/{logger}/{logLevel}")
31+
public ResponseEntity<String> generateLog(@PathVariable("logger") String loggerName,
32+
@PathVariable("logLevel") String logLevel,
33+
@RequestParam(name = "m", required = false, defaultValue = DEFAULT_LOG_MESSAGE) String message) {
34+
Logger logger = LoggerFactory.getLogger(loggerName);
35+
switch (logLevel.toLowerCase()) {
36+
case "error":
37+
logger.error(message);
38+
return ResponseEntity.ok().body("Generated error log with message: \"" + message + "\".");
39+
case "warn":
40+
case "warning":
41+
logger.warn(message);
42+
return ResponseEntity.ok().body("Generated warn log with message: \"" + message + "\".");
43+
case "info":
44+
case "informational":
45+
logger.info(message);
46+
return ResponseEntity.ok().body("Generated info log with message: \"" + message + "\".");
47+
case "debug":
48+
logger.debug(message);
49+
return ResponseEntity.ok().body("Generated debug log with message: \"" + message + "\".");
50+
case "trace":
51+
logger.trace(message);
52+
return ResponseEntity.ok().body("Generated trace log with message: \"" + message + "\".");
53+
}
54+
return ResponseEntity.badRequest().body("Unknows log level \"" + logLevel + "\".");
55+
}
56+
}

0 commit comments

Comments
 (0)