ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 3. SpringBoot + Logback 설정
    Project/React+Java 2022. 7. 21. 17:26

    https://github.com/dchkang83/project-board

     

    GitHub - dchkang83/project-board

    Contribute to dchkang83/project-board development by creating an account on GitHub.

    github.com

     

    1. logback 의존성 추가

    https://mvnrepository.com/search?q=logback

    testImplementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.11'

     

    2. application.yml 설정

    # 파일 삭제
    # /project-board/back-end/gundam/src/main/resources/application.properties
    
    # 파일 추가
    # /project-board/back-end/gundam/src/main/resources/application.yml
    
    
    # server
    server:
      port: 8080
      servlet:
        context-path: /
        # encoding:
        #   charset: UTF-8
        #   enabled: true
        #   force: true
    
    
    
    # log level
    logging:
      logback:
        rollingpolicy:
          file-name-pattern: "/Users/djkang/dev/gundam/product-src/logs/abc.%d{yyyy-MM-dd}.%i"
      pattern:
        file: "[%d{HH:mm:ss.SSS}][%-5level][%logger.%method:line%line] - %msg%n"
      file:
        name: /Users/djkang/dev/gundam/product-src/logs/abc.log
        max-history: 300
      level:
        '[com.main.gundam.sample]': INFO
        org:
          springframework: INFO

     

    3. 로그 확인 코드 추가

    package com.main.gundam.controller;
    
    import org.springframework.http.MediaType;
    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;
    
    import lombok.RequiredArgsConstructor;
    import lombok.extern.slf4j.Slf4j;
    
    // import org.slf4j.Logger;
    // import org.slf4j.LoggerFactory;
    
    @RestController
    @RequestMapping(value = "/main", produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.TEXT_PLAIN_VALUE})
    @RequiredArgsConstructor
    @Slf4j
    public class RestApiController {
    
        // private static final Logger logger = LoggerFactory.getLogger(RestApiController.class);
    
        @GetMapping("home")
        public String home() {
            log.info("home");
            // logger.info("home");
    
            return "<h1>home</h1>";
        }
        
        @GetMapping("/get/{userId}")
        public String get(@PathVariable(value = "userId") final String userId) {
            // logger.info("get user");
            log.info("get user");
    
            return userId;
        }
    }

     

    4. 설정된 경로에 로그파일 생기는지 확인

     

    댓글

Designed by Tistory.