๊ธฐ๋ณธ์ ์ผ๋ก ์ธํฐํ์ด์ค๋ก ๊ตฌ์ฑ, ๊ตฌํ์ฒด๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค
log back ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉ
Thread ์ ๋ณด๋ ํด๋์ค ์ด๋ฆ ๊ฐ์ ๋ถ๊ฐ์ ๋ณด๋ฅผ ํ์ธ
โ ์ด์ ๊น์ง๋ println์ผ๋ก ์ฝ์์ ์ถ๋ ฅํ๋ค๋ฉด ๋ณ๋์ ๋ก๊น
๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํด ๋ก๊ทธ๋ฅผ ์ถ๋ ฅํด์ผํ๋ค
@Slf4j // ๋ก๊น
์ฌ์ฉํ ์ ์๋๋ก ์ธํฐํ์ด์ค ํ์ฉ
@RestController // ์ปจํธ๋กค๋ฌ ๋ง๋ค๊ธฐ
public class Slf4jController {
@RequestMapping("/logging") // ์คํ๋ง๋น ๋ง๋ค๊ธฐ
public String logging() {
String sparta = "Sparta";
// ๋ก๊ทธ ๋ ๋ฒจ : TRACE -> DEBUG -> INFO -> WARN -> ERROR
// ๋ํดํธ : INFO
// โ ์คํ๋์ง ์๋๋ค
log.trace("๋ฌธ์ trace={}", sparta);
log.debug("๋ฌธ์ debug={}", sparta);
// โ
์คํ๋๋ค
log.info("๋ฌธ์ info={}", sparta);
log.warn("๋ฌธ์ warn={}", sparta);
log.error("๋ฌธ์ error={}", sparta);
return "success";
}
}
โ๏ธlog level
- ์ฉ๋ : ์ด ์ค์ ์ ํตํด error๋ง ์ถ๋ ฅํ๋ค๋์ง (์ถ๋ ฅ๋ชจ์ผ๊ธฐ)
๋ก๊ทธ๋ฉ์ธ์ง๋ฅผ ์ผ์๋ณ๋ก ๋ชจ์์ ์ธ๋ถ ์ ์ฅ์์ ๋ณด๊ดํ๊ธฐ๋ ํ๋ค (๋ก๊ทธ ๋ชจ์๋๊ณ ์๋ฌ ์ฐพ๊ธฐ)๐ TRACE > DEBUG > INFO > WARN > ERROR
- ์ ์ฝ๋๋ก ์ค์ ํ ์ ์์ (๋ ๋ฒจ ์์ = TRACE ๋ผ๋ ๋ป)
- ๋ฐ๋ก ์ค์ ํ์ง ์์ผ๋ฉด ๊ธฐ๋ณธ ๋ ๋ฒจ(defalut) : INFO
โ
![]() | โ![]() |
|---|---|
| ์ค๊ดํธ ์ฌ์ฉโญ๏ธ | ์ค๊ดํธ ์ฌ์ฉ โ (๋ฌธ์์ด+๋ฌธ์์ด) ์ฐ์ฐ ํํ ์ฌ์ฉ |
| ์์์ ๊ดํธ๋ ๋ฌธ์๊ฐ ์นํ๋๊ณ ๋ก๊ทธ๋ ๋ฒจ ์ ์ฉ | ๋ก๊ทธ ๋ ๋ฒจ ์๊ด์์ด ์ฐ์ฐ ๋จผ์ ํด๋ฒ๋ฆฐ๋ค |
๐ก ์คํ๋ง์์ ์ปจํธ๋กค๋ฌ๋ฅผ ๋ง๋ค๋ ์ฌ์ฉํ๋ ์ด๋ ธํ ์ด์
@Controller
public class ViewController {
@RequestMapping("/view")
public String example() {
// logic
return "sparta"; // ViewName์ด return๋๋ค (๋ฆฌ์์ค์ ํ์๋ฆฌํ๋ก ๋ง๋ ๋ทฐ)
}
}
*ํ์๋ฆฌํ ์ฌ์ฉ
๊ทธ๋ ์ด๋ค์ ํ์๋ฆฌํ ์์กด์ฑ์ ์ถ๊ฐํ๋ฉด ๋ทฐ์ ๊ธฐ๋ณธ ๊ฒฝ๋ก๊ฐmain/resources/templates๋ก ์ค์ ๋๋ค
โ ์๋ง์ View๋ฅผ ์ฐพ๋ ๊ณผ์ ์์ return๊ฐ์ด string์ด๋ฉด ThymeleafViewResolver์ ์ํด view name์ผ๋ก ์ธ์๋๋ค
@RestController
public class ResponseController {
@RequestMapping("/string")
public String example() {
// logic
return "sparta"; // ViewName์ด return ๋๋๊ฒ ์๋๋ผ, String Data๊ฐ ๋ฐํ๋๋ค.
}
}
*์๋ง๋ View๋ฅผ ์ฐพ๊ณ View Resolverํํ ๊ฐ๋๊ฒ ์๋๋ผ! ๋ฐ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ฐํํ๋ค
(ํ๋ฉด์ด ๊ทธ๋ ค์ง๋๊ฒ ์๋๋ผ ๋ฐ์ดํฐ๊ฐ ๋์จ๋ค)
๐ก @Indexed
๐ก ํน์ URL๋ก ์์ฒญ์ ๋ณด๋ด๋ฉด, ๊ทธ ์์ฒญ๊ณผ ์ปจํธ๋กค๋ฌ ๋ด๋ถ์ ํน์ ๋ฉ์๋๋ฅผ ์ฐ๊ฒฐ / ๋งคํํจ
(์ค์ ๋ก๋ ์ฌ๋ฌ์์๋ฅผ ์กฐํฉํด ๋งคํํ๋ค)
@RequestMapping(value = "/v1", method = RequestMethod.GET)
๋ฒ์ ๋ณ๋ก ์ ์ธ ๋ฐฉ์ ๋ค๋ฅด๋ค! โ ํ์ฌ๋ ๋งจ ๋ค์ ์ฌ๋์ ์์ด์ผํ๋ค
์์ฑ๊ฐ๋ค์ ์ค์ ํ ๋ ๋ฐฐ์ดํํ๋ก ์ ์ธํ๋ฉด ๋ค์ค์ค์ ์ด ๊ฐ๋ฅํ๋ค
๊ธฐ๋ณธ์ ์ผ๋ก ๋ชจ๋ Http ๋ฉ์๋๋ฅผ ํ์ฉํ๋ค (๋ฉ์๋ ์์ฑ ์ค์ ์ํ์๋)
๋ฉ์๋ ์์ฑ์ผ๋ก ๋ฐ๋ก ์ง์ ์ ํด๋ฒ๋ฆฌ๋ฉด ์ง์ ๋ ๊ฒ๋ง ํ์ฉ๋ ๊ฐ๋ฅ
// HTTP Method ๋ GET๋ง ํ์ฉํ๋ค.
@RequestMapping(value = "/v1", method = RequestMethod.GET)
public String exampleV1() {
// logic
return "this is sparta!";
}
@GetMapping(value = "/v2")
method = {RequestMethod.GET} ์ด ์ค์ ๋์ด ์๋ ํด๋์ค@RequestMapping ๋ณด๋ค ์ง๊ด์ ์ด๋ฏ๋ก ์ฃผ๋ก ์ฌ์ฉํ๋ค@GetMapping(value = "/v2")
public String exampleV2() {
// logic
return "this is sparta!";
}
| @PostMapping, @PutMapping, @DeleteMapping, @PatchMapping | @RequestMapping | |
|---|---|---|
| Target | Method Level | Class Level, Method Level |
users/{userId}, category/{categoryId}/product/{productId}๐ก ๊ฒฝ๋ก ๋ณ์์, ๋น์ฐ๊ฒฐ์ฑ์ ๊ทน๋ณตํ๊ธฐ ์ํจ
URL๋ก ์ ๋ฌ๋ ๊ฐ์ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์์จ๋ค
user/{id}
1๏ธโฃ ํ๋ผ๋ฏธํฐ ๋ณ์๋ช ๊ณผ PathVariable ๋ณ์๋ช ์ด ๊ฐ์ผ๋ฉด ์๋ต ๊ฐ๋ฅ
[์๋ต์ โ]
@RequestMapping("/posts")
@RestController
public class PathVariableController {
// postId๋ก ๋ post ๋จ๊ฑด ์กฐํ
@GetMapping("/{postId}")
public String pathVariableV1(@PathVariable("postId") Long data) {
// logic
String result = "PathvariableV1 ๊ฒฐ๊ณผ์
๋๋ค : " + data;
return result;
}
}
---------------
[์๋ตํโญ๏ธ]
@RequestMapping("/posts")
@RestController
public class PathVariableController {
// ๋ณ์๋ช
๊ณผ ๊ฐ๋ค๋ฉด ์์ฑ๊ฐ ์๋ต๊ฐ๋ฅ
@GetMapping("/{postId}")
public String pathVariableV2(@PathVariable Long postId) {
// logic
String result = "PathvariableV2 ๊ฒฐ๊ณผ์
๋๋ค : " + postId;
return result;
}
}
2๏ธโฃ @PathVariable ๋ค์ค ์ฌ์ฉ ๊ฐ๋ฅ
[1๊ฐ ์ฌ์ฉ]
@RestController
public class PathVariableController {
@GetMapping("/{postId}/comments/{commentId}")
public String pathVariableV3(
@PathVariable Long postId,
@PathVariable Long commentId
) {
// logic
String result = "PathvariableV3 ๊ฒฐ๊ณผ์
๋๋ค postId : " + postId + "commentsId : " + commentId;
return result;
}
}
---------
[๋ค์ค ์ฌ์ฉ]
@RequestMapping("/posts/{postId}")
@RestController
public class PathVariableController {
@GetMapping("/comments/{commentId}")
public String pathVariableV4(
@PathVariable Long postId,
@PathVariable Long commentId
) {
// logic
String result = "PathvariableV4 ๊ฒฐ๊ณผ์
๋๋ค postId : " + postId + "commentsId : " + commentId;
return result;
}
}
๐ก ์์ฒญ๊ณผ ์์ฑ์ ํ๋ผ๋ฏธํฐ๋ฅผ ๋งคํํ ์ ์์
@GetMapping(value = "/users", params = "gender=man")
(์์ฒญ :URL GET http://localhost:8080/users?gender=man )
params = "gender"
params = "!gender"
params = "gender=man"
params = "gender!=man"
params = {"gender=man", "gender=woman"}
@PostMapping(value = "/users", headers = "Content-Type=application/json")
ํน์ ํค๋์ ๋งคํํ ์ ์๋ค
์์ฑ ์์ฑ ๊ท์น์ ์ params ์์ฑ ๊ฐ์ ๊ท์น๊ณผ ๊ฐ๋ค
@PostMapping(value = "/users", consumes = "application/json")
consumes ๊ฐ์ //MediaType.APPLICATION_JSON_VALUEMediaType์ Enum์ ์ฌ์ฉํด๋ผ!consumes=โapplication/jsonโ
consumes=โ!application/jsonโ
consumes=โapplication/*โ
consumes=โ*\/*โ
@GetMapping(value = "/users", produces = "text/plain")
MediaType์ Enum์ ์ฌ์ฉํด๋ผ![์์]
// ๋ก๊น
@Slf4j
@RestController
public class RequestHeaderController {
@GetMapping("/request/headers")
public String headers(
HttpServletRequest request, // Servlet์์ ์ฌ์ฉํ๊ฒ๊ณผ ๊ฐ์
HttpServletResponse response, // Servlet์์ ์ฌ์ฉํ๊ฒ๊ณผ ๊ฐ์
@RequestHeader MultiValueMap<String, String> headerMap,
@RequestHeader("host") String host,
@CookieValue(value = "cookie", required = false) String cookie,
HttpMethod httpMethod,
Locale locale
) {
// Servlet
log.info("request={}", request);
log.info("response={}", response);
// @RequestHeader
log.info("headerMap={}", headerMap);
log.info("host={}", host);
// @CookieValue
log.info("cookie={}", cookie);
// HttpMethod
log.info("httpMethod={}", httpMethod);
// Locale
log.info("Locale={}", locale);
return "success";
}
}
API ํธ์ถ

Log ์ถ๋ ฅ ๊ฒฐ๊ณผ
