# 기본 Log 설정 및 Logback 설정

Spring Boot 는 내부로그는 Commons Logging 사용한다.  Java Util Logging 、Log4J2 과 Logback 등 library 들을 자동옵션으로 두었다. 3가지 로그는 모두 IDE 툴 터미널에서 로그를 구체적으로 찍일수 있다. 오늘은 Logback 관련해서 알아보자

<figure><img src="/files/QbbwZ86TdSh7agNW7vS9" alt=""><figcaption></figcaption></figure>

1. 시간일자：ms 단위
2. 로그레벨：ERROR, WARN, INFO, DEBUG or TRACE
3. 프로세스 ID
4. 구분자 ：`---` 로그시작점
5. thread id：
6. Logger名：클래스 명으로 대체&#x20;
7. 로그내용.

로그실제 찍히는 것

```
2021-12-28 17:37:25.578  INFO 65136 --- [           main] com.didispace.chapter81.Application      : Started Application in 2.695 seconds (JVM running for 3.957)
2021-12-28 17:37:25.579 ERROR 65136 --- [           main] com.didispace.chapter81.Application      : Hello World
2021-12-28 17:37:25.579  WARN 65136 --- [           main] com.didispace.chapter81.Application      : Hello World
2021-12-28 17:37:25.579  INFO 65136 --- [           main] com.didispace.chapter81.Application      : Hello World

```

로그모드 변경 방법 2가지

* $ java -jar myapp.jar --debug
* `application.properties 에서 debug=true 속성 추가`

<figure><img src="/files/vt0CyqDmWcH4cMjgbonk" alt=""><figcaption></figcaption></figure>

## 로그설정

### 컬러 설정

application.properties 에 spring.output.ansi.enabled 속성 추가&#x20;

* NEVER : ANSI-colored 사용안함.
* DETECT : ANSI 여부를  판단하고 맞으면 color 로 로그출력&#x20;
* ALWAYS : 항상 ANSI-colored 모드로 출력 ,ANSI 지원하지 않을시 여러가지 "잡"정보가 나오므로 추천하지 않음.&#x20;

{% hint style="info" %}
Spring Boot 1.x 기본 NEVER 이지만 2.x 버전 이후 DETECT 로 변경 됨. 때문에 위 로그 이미지는 기본으로 컬러가 출력 되었다. 요즘 다들 Spring Boot 2.x 를 사용하기때문에 별도로 컬러 설절은 하지 않아도 된다.
{% endhint %}

### 로그파일 만들기&#x20;

application.properties 에&#x20;

```properties
logging.file.name=run.log #파일명
logging.file.path=./      #파일경로
```

### 로그파일 롤링

```properties

logging.logback.rollingpolicy.file-name-pattern: "/logs/abc.%d{yyyy-MM-dd}.%i" #파일 생성 규칙 패턴
logging.logback.rollingpolicy.clean-history-on-start: # 구동시 기존 로그 지우는가 ,기본 false 임.
logging.logback.rollingpolicy.max-history: 30 # 로그파일 유지 개수 
logging.logback.rollingpolicy.max-file-size: # 파일별 사이즈 제한
logging.logback.rollingpolicy.total-size-cap: #
```

### Log Level 컨틀롤

설정 포맷

logging.level.\*=LEVEL

* logging.level : \* 은 패키지 명 혹은 Logger 명
* LEVEL : TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF

예시

<pre class="language-properties"><code class="lang-properties"><strong>
</strong><strong>logging.level.com.didispace=DEBUG # com.blake 패키지하위 모든 class는 DEBUG 레벨사용
</strong>logging.level.root=WARN #root 관련은 로그는 WARN level 사용
</code></pre>

### 커스텀 설정

일반적으로 ApplicationContext 생성서 Log 모듈이 초기화 된다. Spring 설정파일의 영향을 받지 않는다. 때문에 Springboot 외부에서 Log관련 설정을 자유롭게 할수 있다.

아래와 같은 이름을 springboot에서 사용하는데

* Logback：`logback-spring.xml`, `logback-spring.groovy`, `logback.xml`, `logback.groovy`
* Log4j2：`log4j2-spring.xml`, `log4j2.xml`
* JDK (Java Util Logging)：`logging.properties`

SpringBoot 공식사이트에서 -spring 를 붙여서 사용하라고 권장한다.

**`logback.xml 아닌 logback-spring.xml 을 사용해야 된다.`**

**`추가로`** application.properties 에 아래와 같은 속성 값도 설정가능하다.

* logging.pattern.console：터미널에 찍힐 로그 양식 （JDK Logger 안됨.）
* logging.pattern.file：파일에 쓰기위한 로그 양식（JDK Logger 안됨.）

끝!


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blakes-organization.gitbook.io/rainsister/springboot-2.x/log/log-logback.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
