Generate a Springbook application:
$spring init -dweb demo
$ cd demo
Add a new class DemoCtrl.java
cat src/main/java/com/ctrl/DemoCtrl.java
package com.ctrl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/demo/ctrl")
public class DemoCtrl {
@GetMapping
public String demoCtrlGet(){
return "This is a demo - about the RESTFul Get request.";
}
@GetMapping(value = "/gen")
public String generateLog(){
return "Generate log. You can try /gen/echo";
}
@GetMapping(value = "/gen/{echo}")
public String generateLog(@PathVariable String echo){
return echo;
}
}
Add an anotation @ComponentScan(basePackages = {“com.ctrl”}) to class DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = {"com.ctrl"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Project structure:
├── pom.xml
├── REAME.MD
└── src
└── main
├── java
│ └── com
│ ├── ctrl
│ │ └── DemoCtrl.java
│ └── example
│ └── demo
│ └── DemoApplication.java
└── resources
├── application.properties
├── log4j2.xml
├── static
└── templates
Configure original log file path(LogBack)
Change ./src/main/application.properties. Add following line:
logging.file=${sys:user.home}/logback.log
The log will be printed to $HOME/logfile.log
Let take a look at the dependencies:
$ mvn dependency:tree
...
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< com.example:demo >--------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:3.1.1:tree (default-cli) @ demo ---
[INFO] com.example:demo:jar:0.0.1-SNAPSHOT
[INFO] \- org.springframework.boot:spring-boot-starter-web:jar:2.1.4.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter:jar:2.1.4.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot:jar:2.1.4.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.1.4.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-logging:jar:2.1.4.RELEASE:compile
[INFO] | | +- ch.qos.logback:logback-classic:jar:1.2.3