검색결과 리스트
글
!markdown
# Java
## Logback (로그백)
### Example) EvaluatorFilter를 이용해서 특정 로그에 색을 다르게 출력하기
어떻게 로그를 `구분`지을 것인지를 정하고, 출력되는 로그의 문장 또는 특정 값들의 `특징을 파악하고 분석`해야한다.
- 나의 경우에는 다음 4가지의 경우로 구분지었다.
#### 1. 일반 로그
#### 2. 본 프로젝트에서 출력하는 로그 (com.test로 시작)
#### 3. SQL을 출력할때 로그 (com.test.sql로 시작 / package에 "DAO"가 포함 / 로그메세지가"==> Preparing"으로 시작)
#### 4. Batch작업시 로그 (com.test.batch로 시작 )
```xml
<!-- Console Log -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<pattern>[%d] [%highlight(%-5level)] %logger[%cyan(%method:%line)] - %msg %n</pattern>
</encoder>
</appender>
<!-- [all] Develop Console Log -->
<appender name="CONSOLE-DEVELOP" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator> <!-- defaults to type ch.qos.logback.classic.boolex.JaninoEventEvaluator -->
<expression>
if ( logger.startsWith("com.test") || logger.startsWith("com.test.batch") ){
return false;
}
return true;
</expression>
</evaluator>
<OnMismatch>DENY</OnMismatch>
<OnMatch>ACCEPT</OnMatch>
</filter>
<encoder>
<charset>UTF-8</charset>
<pattern>[%d] [%highlight(%-5level)] %logger[%cyan(%method:%line)] - %msg %n</pattern>
</encoder>
</appender>
<!-- [com.test.*] Develop Console Log -->
<appender name="CONSOLE-DEVELOP-TEST" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator> <!-- defaults to type ch.qos.logback.classic.boolex.JaninoEventEvaluator -->
<expression>
if ( logger.startsWith("com.test") && !logger.startsWith("com.test.batch") && (!logger.toUpperCase().contains("DAO") || !message.startsWith("==> Preparing")) ){
return true;
}
return false;
</expression>
</evaluator>
<OnMismatch>DENY</OnMismatch>
<OnMatch>ACCEPT</OnMatch>
</filter>
<encoder>
<charset>UTF-8</charset>
<pattern>[%d] [%highlight(%-5level)] %yellow(%logger)[%cyan(%method:%line)] - %yellow(%msg) %n</pattern>
</encoder>
</appender>
<!-- [com.test.*] Develop Console Log(SQL) -->
<appender name="CONSOLE-DEVELOP-TEST-SQL" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator> <!-- defaults to type ch.qos.logback.classic.boolex.JaninoEventEvaluator -->
<expression>
if ( logger.startsWith("com.test") && !logger.startsWith("com.test.batch") && (logger.toUpperCase().contains("DAO") && message.startsWith("==> Preparing")) ){
return true;
}
return false;
</expression>
</evaluator>
<OnMismatch>DENY</OnMismatch>
<OnMatch>ACCEPT</OnMatch>
</filter>
<encoder>
<charset>UTF-8</charset>
<pattern>[%d] [%highlight(%-5level)] %boldMagenta(%logger)[%cyan(%method:%line)] - %boldMagenta(%msg) %n</pattern>
</encoder>
</appender>
<!-- [com.test.batch*] Develop Console Log(SQL) -->
<appender name="CONSOLE-DEVELOP-TEST-BATCH" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator> <!-- defaults to type ch.qos.logback.classic.boolex.JaninoEventEvaluator -->
<expression>
if ( logger.startsWith("com.test.batch") ){
return true;
}
return false;
</expression>
</evaluator>
<OnMismatch>DENY</OnMismatch>
<OnMatch>ACCEPT</OnMatch>
</filter>
<encoder>
<charset>UTF-8</charset>
<pattern>[%d] [%highlight(%-5level)] %boldBlue(%logger)[%boldBlue(%method:%line)] - %boldBlue(%msg) %n</pattern>
</encoder>
</appender>
<root leve="DEBUG" />
<!--<appender-ref ref="CONSOLE" />-->
<!-- /////////////// -->
<!-- /// Develop /// -->
<!-- /////////////// -->
<appender-ref ref="CONSOLE-DEVELOP" />
<appender-ref ref="CONSOLE-DEVELOP-TEST" />
<appender-ref ref="CONSOLE-DEVELOP-TEST-SQL" />
<appender-ref ref="CONSOLE-DEVELOP-TEST-BATCH" />
</root>
```
### 참고
- Chapter 7: Filters: evalutatorFilter: [https://logback.qos.ch/manual/filters.html#evalutatorFilter](https://logback.qos.ch/manual/filters.html#evalutatorFilter)
'LANGUAGE > Java & Groovy ' 카테고리의 다른 글
[Groovy] try-with-resources ...... withCloseable{} 으로 대체한다? (0) | 2019.08.19 |
---|---|
[Java] try-with-resources (가독성 높이기) (0) | 2019.08.19 |
[Java] Logback - logback-test.xml과 개발용 설정 만들기 (0) | 2018.01.31 |
[Java] Logback - 실행중에 Logger Level 변경 (0) | 2018.01.31 |
[Java] Logback - 모든 로그는 Logger를 통하게 하라. (0) | 2017.11.11 |