general java

[log] log4j에서 json 로그를 출력하도록 설정하기

'김용환' 2018. 3. 26. 17:52



build.gradle에 최신 logstash-logback-encoder를 추가했다.


compile('net.logstash.logback:logstash-logback-encoder:5.0')




https://github.com/logstash/logstash-logback-encoder


Standard Fields

These fields will appear in every LoggingEvent unless otherwise noted. The field names listed here are the default field names. The field names can be customized (see Customizing Standard Field Names).

FieldDescription
@timestampTime of the log event. (yyyy-MM-dd'T'HH:mm:ss.SSSZZ) See customizing timezone.
@versionLogstash format version (e.g. 1) See customizing version.
messageFormatted log message of the event
logger_nameName of the logger that logged the event
thread_nameName of the thread that logged the event
levelString name of the level of the event
level_valueInteger value of the level of the event
stack_trace(Only if a throwable was logged) The stacktrace of the throwable. Stackframes are separated by line endings.
tags(Only if tags are found) The names of any markers not explicitly handled. (e.g. markers from MarkerFactory.getMarker will be included as tags, but the markers from Markers will not.)




기본 포맷은 다음과 같다.


{"@timestamp":"2018-03-26T16:09:15.692+09:00","@version":"1","message":"data","logger_name":"com.kakao.sauron.api.controller.TestController","thread_name":"http-nio-8080-exec-3","level":"INFO","level_value":20000}



보통 caller 관점에서의 line_number도 필요하는데.. 


includeCallerData를 추가하면 관련 정보가 나타난다. 





<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<includeCallerData>true</includeCallerData>
</encoder>




caller 쪽 데이터를 출력할 수 있다.


{"@timestamp":"2018-03-26T16:22:03.196+09:00","@version":"1","message":"data","logger_name":"com.kakao.sauron.api.controller.TestController","thread_name":"http-nio-8080-exec-1","level":"INFO","level_value":20000,"caller_class_name":"com.kakao.sauron.api.controller.TestController","caller_method_name":"helloWorld","caller_file_name":"TestController.java","caller_line_number":28}





너무 많이 나와서.. 필드 이름을 조금 줄일 수 있다. 


<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<includeCallerData>true</includeCallerData>
<fieldNames class="net.logstash.logback.fieldnames.ShortenedFieldNames"/>
</encoder>


{"@timestamp":"2018-03-26T16:28:58.886+09:00","@version":"1","message":"afdsafasfsad","logger":"com.kakao.sauron.api.controller.TestController","thread":"http-nio-8080-exec-1","level":"INFO","levelVal":20000,"caller":{"class":"com.kakao.sauron.api.controller.TestController","method":"helloWorld","file":"TestController.java","line":28}}






이정도가 제일 무난한 정도인 듯 하다.

<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<includeCallerData>true</includeCallerData>
<fieldNames class="net.logstash.logback.fieldnames.ShortenedFieldNames"/>
</encoder>
</appender>
<logger name="jsonLogger" additivity="false" level="DEBUG">
<appender-ref ref="consoleAppender"/>
</logger>
<root level="INFO">
<appender-ref ref="consoleAppender"/>
</root>




물론 여기에 더 해야할 점은 로그 파일의 용량, rotation을 적용해야 한다.

상용에서 사용하려면 더 신경쎠야 한다.