LANGUAGE/Java & Groovy 2017. 11. 11. 21:22

!markdown


# Java


## Logback 컬러링 기능


### 1. Pattern (패턴)


- `%highlight`를 이용하여 로그레벨별로 부각시킬 수 있다.


- `%black, %red, %green, %yellow, %blue, %magenta, %cyan, %white, %gray, %boldRed, %boldGreen, %boldYellow, %boldBlue, %boldMagenta, %boldCyan, %boldWhite`를 이용하여 특정구간의 색을 정할 수 있다.


- 컬러패턴 다음에는 `( )`, 즉 `sub-pattern을 이용하여 구간을 정한다.`


- `Windows`의 `cmd`에서는 작동하지 않습니다. 하지만, `jansi`라이브러리를 추가하면,  `cmd`에서도 작동합니다. 


### 2. Example (예제) 


- logback.xml

```xml

    <configuration debug="true">


      <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">

        <!-- On Windows machines setting withJansi to true enables ANSI

             color code interpretation by the Jansi library. This requires

             org.fusesource.jansi:jansi:1.8 on the class path.  Note that

             Unix-based operating systems such as Linux and Mac OS X

             support ANSI color codes by default. -->

        <withJansi>true</withJansi>

        <encoder>

          <pattern>[%thread] %highlight(%-5level) %cyan(%logger{15}) - %msg %n</pattern>

        </encoder>

      </appender>


      <root level="DEBUG">

        <appender-ref ref="STDOUT" />

      </root>


    </configuration>

```


- Gradle (build.gradle)

```

compile 'org.fusesource.jansi:jansi:1.16'

```


### 3. 참조 

  - Logback - Coloring: [https://logback.qos.ch/manual/layouts.html#coloring](https://logback.qos.ch/manual/layouts.html#coloring)