-
[Spring] 게시판 만들기(22) - MDC(Mapped Diagnostic Context) 설정Spring/4.3.x - 게시판 만들기 2019. 11. 24. 13:37반응형
MDC를 설정하는 이유는 웹 요청에 대한 로그인 정보나 세션 정보를 추적할 수 있기 때문입니다. 현재 샘플에서는 로그인 정보나 세션 정보가 없기 때문에 BOARD_SEQ 값을 추가하였습니다.
1.logback.xml 수정
key에 대한 value 값만 노출하고 싶다면 패턴에 %X{ID}로 추가한 MDC에 key값을 ID로 정의하면 되고('ID'는 임의로 정의한 값), key_value 패턴으로 key와 value 값을 모두 노출하고 싶다면 %X로 추가하면 됩니다. (%X은 로깅이 발생한 Thread와 관련된 MDC에 정보를 출력합니다.)
더보기12345678910111213141516171819202122<?xml version="1.0" encoding="UTF-8"?><configuration><appender name="console" class="ch.qos.logback.core.ConsoleAppender"><layout class="ch.qos.logback.classic.PatternLayout"><pattern>%d [%thread] %-5level %logger - [%X{ID}]:%msg%n</pattern></layout></appender><logger name="org.springframework" level="info" additivity="false"><appender-ref ref="console"/></logger><logger name="com.spring.board" level="debug" additivity="false"><appender-ref ref="console"/></logger><root level="info"><appender-ref ref="console"/></root></configuration>cs 2. BoardController.java에 MDC 추가
MDC.put(key, value), MDC.remove(key)을 설정하세요.
더보기123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142package com.spring.board.controller;import java.util.HashMap;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.slf4j.MDC;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.ModelAndView;import com.spring.board.common.ResultUtil;import com.spring.board.dto.BoardDto;import com.spring.board.form.BoardForm;import com.spring.board.service.BoardService;@Controller@RequestMapping(value = "/board")public class BoardController {@Autowiredprivate BoardService boardService;/** 게시판 - 목록 페이지 이동 */@RequestMapping(value = "/boardList")public String boardList(HttpServletRequest request, HttpServletResponse response) throws Exception {return "board/boardList";}/** 게시판 - 목록 조회 */@RequestMapping(value = "/getBoardList")@ResponseBodypublic ResultUtil getBoardList(HttpServletRequest request, HttpServletResponse response, BoardForm boardForm) throws Exception {ResultUtil resultUtils = boardService.getBoardList(boardForm);return resultUtils;}/** 게시판 - 상세 페이지 이동 */@RequestMapping(value = "/boardDetail")public String boardDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {return "board/boardDetail";}/** 게시판 - 상세 조회 */@RequestMapping(value = "/getBoardDetail")@ResponseBodypublic BoardDto getBoardDetail(HttpServletRequest request, HttpServletResponse response, BoardForm boardForm) throws Exception {MDC.put("ID", String.valueOf(boardForm.getBoard_seq()));BoardDto boardDto = boardService.getBoardDetail(boardForm);MDC.remove("ID");return boardDto;}/** 게시판 - 작성 페이지 이동 */@RequestMapping(value = "/boardWrite")public String boardWrite(HttpServletRequest request, HttpServletResponse response) throws Exception {return "board/boardWrite";}/** 게시판 - 등록 */@RequestMapping(value = "/insertBoard")@ResponseBodypublic BoardDto insertBoard(HttpServletRequest request, HttpServletResponse response, BoardForm boardForm) throws Exception {BoardDto boardDto = boardService.insertBoard(boardForm);return boardDto;}/** 게시판 - 삭제 */@RequestMapping(value = "/deleteBoard")@ResponseBodypublic BoardDto deleteBoard(HttpServletRequest request, HttpServletResponse response, BoardForm boardForm) throws Exception {BoardDto boardDto = boardService.deleteBoard(boardForm);return boardDto;}/** 게시판 - 수정 페이지 이동 */@RequestMapping(value = "/boardUpdate")public String boardUpdate(HttpServletRequest request, HttpServletResponse response) throws Exception {return "board/boardUpdate";}/** 게시판 - 수정 */@RequestMapping(value = "/updateBoard")@ResponseBodypublic BoardDto updateBoard(HttpServletRequest request, HttpServletResponse response, BoardForm boardForm) throws Exception {BoardDto boardDto = boardService.updateBoard(boardForm);return boardDto;}/** 게시판 - 답글 페이지 이동 */@RequestMapping(value = "/boardReply")public String boardReply(HttpServletRequest request, HttpServletResponse response) throws Exception {return "board/boardReply";}/** 게시판 - 답글 등록 */@RequestMapping(value = "/insertBoardReply")@ResponseBodypublic BoardDto insertBoardReply(HttpServletRequest request, HttpServletResponse response, BoardForm boardForm) throws Exception {BoardDto boardDto = boardService.insertBoardReply(boardForm);return boardDto;}/** 게시판 - 첨부파일 다운로드 */@RequestMapping("/fileDownload")public ModelAndView fileDownload(@RequestParam("fileNameKey") String fileNameKey,@RequestParam("fileName") String fileName,@RequestParam("filePath") String filePath) throws Exception {/** 첨부파일 정보 조회 */Map<String, Object> fileInfo = new HashMap<String, Object>();fileInfo.put("fileNameKey", fileNameKey);fileInfo.put("fileName", fileName);fileInfo.put("filePath", filePath);return new ModelAndView("fileDownloadUtil", "fileInfo", fileInfo);}}cs 3. BoardService.java에 logger 추가
logger 정보를 추가하세요.
더보기123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267package com.spring.board.service;import java.io.File;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.UUID;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.web.multipart.MultipartFile;import com.spring.board.common.PagingUtil;import com.spring.board.common.ResultUtil;import com.spring.board.dao.BoardDao;import com.spring.board.dto.BoardDto;import com.spring.board.dto.CommonDto;import com.spring.board.form.BoardFileForm;import com.spring.board.form.BoardForm;import com.spring.board.form.CommonForm;@Servicepublic class BoardService {protected final Logger logger = LoggerFactory.getLogger(BoardService.class);@Autowiredprivate BoardDao boardDao;/** 게시판 - 목록 조회 */public ResultUtil getBoardList(BoardForm boardForm) throws Exception {ResultUtil resultUtil = new ResultUtil();CommonDto commonDto = new CommonDto();int totalCount = boardDao.getBoardCnt(boardForm);if (totalCount != 0) {CommonForm commonForm = new CommonForm();commonForm.setFunction_name(boardForm.getFunction_name());commonForm.setCurrent_page_no(boardForm.getCurrent_page_no());commonForm.setCount_per_page(10);commonForm.setCount_per_list(10);commonForm.setTatal_list_count(totalCount);commonDto = PagingUtil.setPageUtil(commonForm);}boardForm.setLimit(commonDto.getLimit());boardForm.setOffset(commonDto.getOffset());List<BoardDto> list = boardDao.getBoardList(boardForm);HashMap<String, Object> resultMap = new HashMap<String, Object>();resultMap.put("list", list);resultMap.put("totalCount", totalCount);resultMap.put("pagination", commonDto.getPagination());resultUtil.setData(resultMap);resultUtil.setState("SUCCESS");return resultUtil;}/** 게시판 - 목록 조회 *//** public List<BoardDto> getBoardList(BoardForm boardForm) throws Exception {** return boardDao.getBoardList(boardForm); }*//** 게시판 - 상세 조회 */public BoardDto getBoardDetail(BoardForm boardForm) throws Exception {logger.debug("==================== getBoardDetail START ====================");BoardDto boardDto = new BoardDto();String searchType = boardForm.getSearch_type();if ("S".equals(searchType)) {int updateCnt = boardDao.updateBoardHits(boardForm);if (updateCnt > 0) {boardDto = boardDao.getBoardDetail(boardForm);}} else {boardDto = boardDao.getBoardDetail(boardForm);}BoardFileForm boardFileForm = new BoardFileForm();boardFileForm.setBoard_seq(boardForm.getBoard_seq());boardDto.setFiles(boardDao.getBoardFileList(boardFileForm));logger.debug("==================== getBoardDetail END ====================");return boardDto;}/** 게시판 - 등록 */public BoardDto insertBoard(BoardForm boardForm) throws Exception {BoardDto boardDto = new BoardDto();int insertCnt = 0;int boardReRef = boardDao.getBoardReRef(boardForm);boardForm.setBoard_re_ref(boardReRef);insertCnt = boardDao.insertBoard(boardForm);List<BoardFileForm> boardFileList = getBoardFileInfo(boardForm);for (BoardFileForm boardFileForm : boardFileList) {boardDao.insertBoardFile(boardFileForm);}if (insertCnt > 0) {boardDto.setResult("SUCCESS");} else {boardDto.setResult("FAIL");}return boardDto;}/** 게시판 - 첨부파일 정보 조회 */public List<BoardFileForm> getBoardFileInfo(BoardForm boardForm) throws Exception {List<MultipartFile> files = boardForm.getFiles();List<BoardFileForm> boardFileList = new ArrayList<BoardFileForm>();BoardFileForm boardFileForm = new BoardFileForm();int boardSeq = boardForm.getBoard_seq();String fileName = null;String fileExt = null;String fileNameKey = null;String fileSize = null;// 파일이 저장될 Path 설정String filePath = "C:\\board\\file";if (files != null && files.size() > 0) {File file = new File(filePath);// 디렉토리가 없으면 생성if (file.exists() == false) {file.mkdirs();}for (MultipartFile multipartFile : files) {fileName = multipartFile.getOriginalFilename();fileExt = fileName.substring(fileName.lastIndexOf("."));// 파일명 변경(uuid로 암호화) + 확장자fileNameKey = getRandomString() + fileExt;fileSize = String.valueOf(multipartFile.getSize());// 설정한 Path에 파일 저장file = new File(filePath + "/" + fileNameKey);multipartFile.transferTo(file);boardFileForm = new BoardFileForm();boardFileForm.setBoard_seq(boardSeq);boardFileForm.setFile_name(fileName);boardFileForm.setFile_name_key(fileNameKey);boardFileForm.setFile_path(filePath);boardFileForm.setFile_size(fileSize);boardFileList.add(boardFileForm);}}return boardFileList;}/** 게시판 - 삭제 */public BoardDto deleteBoard(BoardForm boardForm) throws Exception {BoardDto boardDto = new BoardDto();int deleteCnt = boardDao.deleteBoard(boardForm);if (deleteCnt > 0) {boardDto.setResult("SUCCESS");} else {boardDto.setResult("FAIL");}return boardDto;}/** 게시판 - 수정 */public BoardDto updateBoard(BoardForm boardForm) throws Exception {BoardDto boardDto = new BoardDto();int updateCnt = boardDao.updateBoard(boardForm);String deleteFile = boardForm.getDelete_file();if (!"".equals(deleteFile)) {String[] deleteFileInfo = deleteFile.split("!");int boardSeq = Integer.parseInt(deleteFileInfo[0]);int fileNo = Integer.parseInt(deleteFileInfo[1]);BoardFileForm deleteBoardFileForm = new BoardFileForm();deleteBoardFileForm.setBoard_seq(boardSeq);deleteBoardFileForm.setFile_no(fileNo);boardDao.deleteBoardFile(deleteBoardFileForm);}List<BoardFileForm> boardFileList = getBoardFileInfo(boardForm);for (BoardFileForm boardFileForm : boardFileList) {boardDao.insertBoardFile(boardFileForm);}if (updateCnt > 0) {boardDto.setResult("SUCCESS");} else {boardDto.setResult("FAIL");}return boardDto;}/** 게시판 - 답글 등록 */public BoardDto insertBoardReply(BoardForm boardForm) throws Exception {BoardDto boardDto = new BoardDto();BoardDto boardReplayInfo = boardDao.getBoardReplyInfo(boardForm);boardForm.setBoard_seq(boardReplayInfo.getBoard_seq());boardForm.setBoard_re_lev(boardReplayInfo.getBoard_re_lev());boardForm.setBoard_re_ref(boardReplayInfo.getBoard_re_ref());boardForm.setBoard_re_seq(boardReplayInfo.getBoard_re_seq());int insertCnt = 0;insertCnt += boardDao.updateBoardReSeq(boardForm);insertCnt += boardDao.insertBoardReply(boardForm);if (insertCnt > 0) {boardDto.setResult("SUCCESS");} else {boardDto.setResult("FAIL");}return boardDto;}/** 32글자의 랜덤한 문자열(숫자포함) 생성 */public static String getRandomString() {return UUID.randomUUID().toString().replaceAll("-", "");}}cs 4. MDC 확인
로그에서 설정한 MDC 정보가 출력되는지 확인하세요.
소스 코드는 Github Repository - https://github.com/tychejin1218/board_v1 (branch : section22) 를 참조하세요.
Github에서 프로젝트 가져오기 - https://tychejin.tistory.com/33반응형'Spring > 4.3.x - 게시판 만들기' 카테고리의 다른 글
[Spring] 게시판 만들기(21) - 게시글 수정(첨부파일 등록 및 삭제) (5) 2019.05.10 [Spring] 게시판 만들기(20) - 파일 다운로드(BeanNameViewResolver) (9) 2018.12.20 [Spring] 게시판 만들기(19) - 파일 업로드(MultipartHttpServletRequest) (2) 2018.12.20 [Spring] 게시판 만들기(18) - 계층형 게시판(답글쓰기) 적용 (11) 2018.12.19 [Spring] 게시판 만들기(17) - 페이징(Paging) 처리 (31) 2018.12.19