5. 로깅처리
https://docs.spring.io/spring-boot/docs/2.7.18/reference/htmlsingle/#features.logging
# 로깅처리시 색상변경 안먹힐때 해결방법
https://stackoverflow.com/questions/48011632/where-is-spring-output-ansi-enabled-configured
1> 개요
- System.out.println 대신 특정 값(로그값)을 상황(레벨;단계)에 맞게 콘솔(파일)에 출력.
2> 로그처리를 전문적으로 해주는 라이브러리
- log4J 라이브러리 ==> log4jPrint()
- logback 라이브러리(boot 기본 로깅담당) ==> logbackPrint()
* 로깅처리 구조
SLF4J (인터페이스, 스펙) - log()
|
| 구현
log4J logback
3> 로깅레벨
trace
debug
info(기본)
warn
error
==> application.properties 에 로깅레벨을 설정
==> 동작방식은 지정된 레벨 포함한 하위 레벨까지 로깅처리됨.
4> boot 에서 로깅처리를 담당하는 의존성이 있음.
spring-boot-starter 의 서브로 spring-boot-starter-logging 이 담당
spring-boot-starter-logging 에는 log4J, logback, slf4j 가 있다.
5> 적용
가. application.properties 에 로깅레벨을 설정
문법:
logging.level.관심있는패키지명=로그레벨
# 로깅레벨
logging.level.org.springframework=trace
(org.springframework 패키지는 boot 에서 제공해준것임.)
# 로깅레벨 - 패키지명지정
logging.level.com.exam=info
# 파일에 저장1 - 경로지정만 함.
logging.file.path=c://log (spring.log 파일이 생성됨.)
# 파일에 저장2 (경로지정 + 파일명 같이 지정)
logging.file.name=c:\\temp\\test2.log
6> 사용자 지정 로그 출력
# application.properties
logging.level.com.exam = true
# java
import org.slf4j.Logger;
Logger logger = LoggerFactory.getLogger(getClass());
// 로그출력
logger.trace("trace: {}, {}", "trace1", "trace2");
logger.debug("debug: {}", "debug");
logger.info("info: {}", "info"); // 기본이 info 라서 info, warn, error 까지는 나옴
logger.warn("warn: {}", "warn");
logger.error("error: {}", "error");
package com.exam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
// slf4j.Logger 로 해야한다. - 상위레벨인 인터페이스 이기 때문.(다른 구현체를 사용하더라도 변경 필요가 없기때문)
Logger logger = LoggerFactory.getLogger(getClass()); // 로그메서드를 출력해주는 로거
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
// System.out.println("main"); // 여기서 작업해도 되지만 implements CommandLineRunner 해서
// 아래의 run 메서드를 사용한다.
}
@Override
public void run(String... args) throws Exception {
// TODO Auto-generated method stub
// System.out.println("main");
// 로그 출력(전에 로거를 생성해야한다.)
// trace("trace: {값1 이 들어감}, {값2 가 들어감}", "값1", "값2") - printf 와 유사
logger.trace("trace: {}, {}", "trace1", "trace2");
logger.debug("debug: {}", "debug");
logger.info("info: {}", "info"); // 기본이 info 라서 info, warn, error 까지는 나옴
logger.warn("warn: {}", "warn");
logger.error("error: {}", "error");
}
}
# application.properties
# 로깅레벨
#logging.level.org.springframework=error
logging.level.com.exam=trace
# log 폴더 생성 그 후 spring.log 파일명으로 저장됨.
#logging.file.path=c://lo
logging.file.name=c:\\temp\\test2.log
# ALWAYS|DETECT|NEVER
spring.output.ansi.enabled=never
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.18</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.exam</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
'Programming > Spring Boot' 카테고리의 다른 글
빈의 scope (0) | 2024.07.02 |
---|---|
생성된 빈 접근방법 (0) | 2024.07.02 |
의존성 설정 (0) | 2024.07.02 |
빈(Bean) 생성 (0) | 2024.07.02 |
개요 (0) | 2024.07.02 |