1 ๋ถ„ ์†Œ์š”

์˜ˆ์™ธ์ฒ˜๋ฆฌ ์‹œ ๋กœ๊ทธ ๋‚จ๊ธฐ๊ธฐ

์„œ๋น„์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋‹ค๋ณด๋ฉด ์—ฌ๋Ÿฌ ์˜ค๋ฅ˜์— ๋ถ€๋”ชํžŒ๋‹ค.
์ด ๋•Œ, ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค๋ฉด ์–ด๋–ค ๋ถ€๋ถ„์—์„œ ์–ด๋–ค ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ์ธ์ง€ ์‰ฝ๊ฒŒ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ธํ„ด ๊ธฐ๊ฐ„๋™์•ˆ ์ง„ํ–‰ํ•œ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” org.slf4j.Logger, org.slf4j.LoggerFactory ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.
์ด ์™ธ์—๋„ java.util.logging ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์กด์žฌํ•œ๋‹ค.
๋‘ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๊ฐœ์ธ ์„ ํ˜ธ๋„ ์ฐจ์ด์ง€๋งŒ, slf4j ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ logging์— ๋น„ํ•ด ๋กœ๊น…์— ๋Œ€ํ•œ ์œ ์—ฐ์„ฑ์ด ํฌ๊ณ , ๋‹ค์–‘ํ•œ ๋ฐฑ์—”๋“œ๋ฅผ ์ง€์›ํ•˜๋Š” ๋“ฑ์˜ ํŠน์ง• ๋•๋ถ„์— ๋งŽ์€ ํ”„๋กœ์ ํŠธ์—์„œ ์„ ํ˜ธํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ผ๊ณ  ํ•œ๋‹ค.

์–ด๋””์— ์‚ฌ์šฉํ•˜์ง€?

ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉฐ Logger๋ฅผ ์–ด๋””์— ์‚ฌ์šฉํ•˜๋Š”์ง€ ์„ ์ž„ ๊ฐœ๋ฐœ์ž๋ถ„๋“ค์˜ ์ฝ”๋“œ๋ฅผ ์ฐธ์กฐํ•˜์—ฌ ์•Œ ์ˆ˜ ์žˆ์—ˆ๋‹ค.
๋จผ์ €, Logger๋ฅผ ์„ ์–ธํ•ด์ค˜์•ผ ํ•œ๋‹ค.

Logger log = LoggerFactory.getLogger(ProductController.class);


[์˜ˆ์‹œ 1]

    /**
	 * product ์ˆ˜์ •(Update) Controller
	 */
	@ResponseBody
	@RequestMapping("update")
	public String update(ProductVO voData, HttpSession session) {
		
		String resultCode = "success";

		// update
		try {
			productService.updateProduct(voData);
		} catch (Exception e) {
			log.error("update product failed.", e);
			resultCode = "failed";
		}
		
		return resultCode;
	}   

์œ„์™€ ๊ฐ™์€ ์˜ˆ์‹œ์—์„œ ๋ณด๋ฉด, ๋ณ€์ˆ˜ ์„ ์–ธ๊ณผ ๊ฐ™์€ ๋ถ€๋ถ„์€ try ~ catch ๊ตฌ๋ฌธ ๋ฐ–์—์„œ ์ง„ํ–‰ํ•˜๊ณ  Service(Impl)์— ์ ‘๊ทผํ•˜๋Š” ๋กœ์ง๋ถ€ํ„ฐ๋Š” try ~ catch ๊ตฌ๋ฌธ์œผ๋กœ ๊ฐ์‹ธ๊ณ  ์žˆ๋‹ค.
Logger๋Š” try ~ catch ๊ตฌ๋ฌธ์—์„œ ์‚ฌ์šฉํ•˜๋ฉฐ, catch(Exception e)์™€ ๊ฐ™์ด ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด Exception์œผ๋กœ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.
์ด ๋•Œ, log.error() ๋ฉ”์†Œ๋“œ์— ์˜ˆ์™ธ์ฒ˜๋ฆฌ ๋ณ€์ˆ˜๋ฅผ ๋„ฃ์–ด์ฃผ์–ด ์˜ˆ์™ธ ์ƒํ™ฉ์„ ๋กœ๊ทธ(log)๋กœ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

[์˜ˆ์‹œ 2]

    /**
	 * product ์ˆ˜์ •(Update) Controller
	 */
	@ResponseBody
	@RequestMapping("update")
	public String update(ProductVO voData, HttpSession session) {
		
		String resultCode = "success";

		// update
		try {
			productService.updateProduct(voData);
		} catch (Exception e) {
			e.printStackTrace();
			resultCode = "failed";
		}
		
		return resultCode;
	}   

์œ„์—์„œ๋Š” log.error()๊ฐ€ ์•„๋‹Œ e.printStackTrace() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ํ–ˆ๋‹ค.

์ด ๋‘˜์˜ ์ฐจ์ด์ ์€ ๋ญ˜๊นŒ?

log.error() vs e.printStackTrace()

log.error()๋Š” ๋ง ๊ทธ๋Œ€๋กœ error ๋ฐœ์ƒ ์‹œ, ์ฝ˜์†”์— ๋กœ๊ทธ๋ฅผ ์ฐ์–ด์ฃผ๋Š” ๋ฐฉ์‹์ด๋‹ค.
์—ฌ๊ธฐ์„œ๋Š” log.error()๋งŒ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ, ์ด ๋ฉ”์†Œ๋“œ๋Š” ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์ค‘ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ ์˜ค๋ฅ˜ ์ •๋ณด๋ฅผ ์ฝ˜์†”์— ์ฐ์–ด์ฃผ๋Š” ๋ฐฉ์‹์ด๋‹ค.
log ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋กœ๊ทธ๋ ˆ๋ฒจ์„ ํ†ตํ•ด ์›ํ•˜๋Š” ์ˆ˜์ค€์˜ ๋กœ๊ทธ๊นŒ์ง€๋งŒ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.


e.printStackTrace()๋Š” ์—๋Ÿฌ์˜ ๋ฐœ์ƒ ๊ทผ์›์ง€๋ฅผ ์ฐพ์•„ ๋‹จ๊ณ„๋ณ„๋กœ ์—๋Ÿฌ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.
์ด๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ์ƒํ™ฉ์„ ๋ถ„์„ํ•˜๊ธฐ ์ข‹๊ฒŒ ๋„์™€์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๋ ค ๋””๋ฒ„๊น…์„ ํ•  ์ˆ˜ ์žˆ๋Š” ํžŒํŠธ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
ํ•˜์ง€๋งŒ, e.printStackTrace()๋ฅผ ์ง€์–‘ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค๊ณ  ํ•˜๋Š”๋ฐ ์™œ ๊ทธ๋Ÿด๊นŒ?

์™œ e.printStackTrace() ์ง€์–‘ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„๊นŒ?

  1. printStackTrace()๋ฅผ callํ•  ๊ฒฝ์šฐ System.err๋กœ ์“ฐ์—ฌ์ ธ ์ œ์–ด๊ฐ€ ํž˜๋“ค๋‹ค.
  2. printStackTrace()๋Š” java ๋ฆฌํ”Œ๋ ‰์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์ถ”์ ํ•˜๋Š” ๊ฒƒ -> ๋งŽ์€ ์˜ค๋ฒ„ํ—ค๋“œ ๋ฐœ์ƒ ๊ฐ€๋Šฅ.
  3. printStackTrace()๋Š” ์„œ๋ฒ„์—์„œ ์Šคํƒ์ •๋ณด๋ฅผ ์ทจํ•ฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋ฒ„์— ๋ถ€ํ•˜๊ฐ€ ์ƒ๊ธธ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์Œ.
  4. printStackTrace()๋Š” ์ถœ๋ ฅ์ด ์–ด๋””๋กœ ๊ฐ€๋Š”์ง€ ํŒŒ์•…์ด ์–ด๋ ค์›€.(tomcat์˜ ๊ฒฝ์šฐ catalina.out์— ๋‚จ์Œ)


์„ฑ๋Šฅ์„ ์ค‘์š”ํ•˜๊ฒŒ ์—ฌ๊ธฐ๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด๋ผ๋ฉด e.printStackTrace()๋Š” ํ”ผํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

์ฐธ๊ณ ์ž๋ฃŒ

https://tgyun615.com/59

https://developsd.tistory.com/132

์šด์˜ ํ™˜๊ฒฝ์—์„œ ์˜ค๋ฅ˜ ๊ฒ€์ถœํ•˜๊ธฐ [Log, Logging ๊ฐœ์š”, Log์˜ ํŠน์ง•]

ํƒœ๊ทธ: ,

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ