상위 질문
타임라인
채팅
관점
Log4j
자바 기반의 로깅 소프트웨어 위키백과, 무료 백과사전
Remove ads

Log4j는 Ceki Gülcü가 처음 개발한 자바 기반 로깅 유틸리티이다. 아파치 소프트웨어 재단의 프로젝트 아파치 로깅 서비스(Apache Logging Services)의 일부이다. 또, Log4j는 여러 자바 로깅 프레임워크들 가운데 하나이다. 최종 사용자가 제품의 문제나 정보를 식별하기 위해, 그리고 소프트웨어 개발자가 프로그램을 개발하는 도중에 디버깅 등을 위해 타임스탬프 등 정해진 양식에 맞추어 화면 상이나 파일로 로그를 남길 목적으로 사용된다.
Remove ads
그 뒤로 Gülcü는 Log4j의 뒤를 잇는 유틸리티를 제공할 목적으로 SLF4J와 로그백(Logback)[4] 프로젝트를 시작했다.
아파치 Log4j 팀은 Log4j 1의 후속으로 버전 번호 2를 붙인 Log4j 2를 개발해오고 있다.[5] Log4j 2는 Log4j 1.2, 1.3, java.util.logging
, 로그백(Logback)의 문제점들에 초점을 두고 개발되었으며 해당 프레임워크에서 나타난 여러 문제들을 해결한다.[6] 게다가 Log4j 2는 전작에 비해 더 확장성을 높이기 위한 플러그인 아키텍처를 채택하고 있다. Log4j 2는 1.x 버전들과 하위 호환이 되지는 않으나[7] 어댑터(adapter)의 이용은 가능하다. 2015년 8월 5일, 아파치 로깅 서비스 프로젝트 관리 위원회는 Log4j 1이 수명 만기에 도달했으며 Log4j 1 사용자들은 아파치 Log4j 2로 업그레이드할 것을 권고한다고 발표하였다.[8]
2021년 12월 9일, 제로 데이 임의 코드 실행 취약점이 Log4j 2에서 보고되었다.[9] 지난 10년 내의 가장 크고 가장 심각한 취약점이다.[10]
Remove ads
아파치 Log4j 2
아파치 Log4j 2는 Log4j 1의 뒤를 잇는다. 이 프레임워크는 처음부터 재개발되었으며 Log4j 1, java.util.logging을 포함한 기존 로깅 솔루션들의 영향을 받아 만들어졌다. Log4j 1 대비 주된 차이점[11][12]은 다음과 같다:
- 개선된 가독성. Log4j 1이나 로그백과 같은 프레임워크를 재구성하는 동안 메시지는 소실되지 않음.
- 확장성. Log4j 2는 플러그인 시스템을 지원하므로 사용자들이 사용자 지정 구성 요소를 정의하고 구성할 수 있음.
- 단순화된 구성 문법
- xml, json, yaml, properties 구성 지원
- 개선된 필터
- 구성 파일, 시스템 속성, 환경 변수, ThreadContext Map, 이벤트 내 데이터에 정의된 값의 속성 찾기 지원
- 여러 API 지원: Log4j 2는 Log4j 2, Log4j 1.2, SLF4J, Commons Logging, java.util.logging (JUL) API를 사용하여 응용 프로그램과 함께 사용할 수 있음.
- 사용자 지정 로그 레벨
- 레이지 로깅(lazy logging)을 위한 자바 8 스타일의 람다 지원
- 마커
- 사용자 지정 메시지 오브젝트 지원
- 공통 구성에서 가비지가 거의 없거나 아예 없음
- 개선된 속도
Log4j 2의 가장 눈에 띄는 기능들 가운데 하나는 비동기 Loggers의 성능이다.[13] Log4j 2는 LMAX 디스럽터를 활용한다.[14] 이 라이브러리는 커널 락의 필요성을 줄이며 12배만큼 로깅 성능을 제고한다. 이를테면 동일 환경에서 Log4j 2는 1초에 18,000,000개 이상의 메시지를 기록할 수 있는 반면 로그백과 Log4j 1 등의다른 프레임워크들은 초당 2,000,000개 미만의 메시지를 기록할 수 있다.
Remove ads
기능
요약
관점
로그 레벨
log4j의 최근 버전에 의하면 높은 등급에서 낮은 등급으로의 7개 로그 레벨을 가지고 있다. 설정 파일에 대상별(자바에서는 패키지)로 레벨을 지정이 가능하고 그 등급 이상의 로그만 저장하는 방식이다.
사용자 지정 로그 레벨
Log4j 2는 사용자가 자신만의 로그 레벨을 정의할 수 있게 허용한다.[16] 내장된 로그 레벨과 동일한 사용자 지정 로그 레벨을 지원하는 Loggers를 만들기 위해 소스 코드 생성기 도구를 제공한다.
Log4j 구성
Log4j는 구성 파일 또는 자바 코드를 통해 구성할 수 있다.[17] XML, JSON, YAML 또는 properties 파일 포맷으로 작성이 가능하다. 구성 내에서 사용자는 3개의 주요 구성 요소를 정의할 수 있다: Loggers, Appenders, Layouts. 파일을 통해 로깅을 구성할 경우 Log4j를 사용하는 응용 프로그램을 수정하지 않더라도 로깅을 켜고 끌 수 있다는 장점이 있다. 응용 프로그램은 문제가 생길 때까지 로그를 끄도록 허용하는 것이 가능한데, 예를 들어 로깅을 되돌리거나 단순히 구성 파일을 수정하는 것만으로 가능하다.
- Loggers는 로그 메시지 도착지 이름이다.[18]
- Appenders는 실제 출력을 수행하는 실체이다.[19]
- Layouts는 Appenders가 로그 항목의 서식을 지정하기 위해 사용한다.[20]
- Filters는 Log4j 2의 기능으로, 어느 로그 항목이 어느 Loggers와 Appenders에 의해 처리되어야 하는지를 세밀히 조절하기 위한 것이다.[21]
Log4j 2의 예시
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace" monitorInterval="60">
<Properties>
<Property name="filename">target/test.log</Property>
</Properties>
<Appenders>
<Console name="STDOUT">
<PatternLayout pattern="%d %p %c{1.} [%t] %m%n"/>
</Console>
<File name="file" fileName="${filename}">
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<!--
loggers whose name starts with 'org.springframework' will only log messages of level "info" or higher;
if you retrieve Loggers by using the class name (e.g. Logger.getLogger(AClass.class))
and if AClass is part of the org.springframework package, it will belong to this category
-->
<Logger name="org.springframework" level="info" additivity="false" />
<!--
Filter example: for loggers whose name starts with 'com.mycompany.myproduct',
log entries of level "debug" or higher whose ThreadContextMap data contains
the key-value pair "test=123", also send these log entries to the "STDOUT" appender.
-->
<Logger name="com.mycompany.myproduct" level="debug" additivity="true">
<ThreadContextMapFilter>
<KeyValuePair key="test" value="123"/>
</ThreadContextMapFilter>
<AppenderRef ref="STDOUT"/>
</Logger>
<!--
By default, all log messages of level "trace" or higher will be logged.
Log messages are sent to the "file" appender and
log messages of level "error" and higher will be sent to the "STDOUT" appender.
-->
<Root level="trace">
<AppenderRef ref="file"/>
<AppenderRef ref="STDOUT" level="error"/>
</Root>
</Loggers>
</Configuration>
Log4j 1.2의 예시
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//LOGGER" "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration>
<!--
an appender is an output destination, such as the console or a file;
names of appenders are arbitrarily chosen.
-->
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n" />
</layout>
</appender>
<!--
loggers of category 'org.springframework' will only log messages of level "info" or higher;
if you retrieve Loggers by using the class name (e.g. Logger.getLogger(AClass.class))
and if AClass is part of the org.springframework package, it will belong to this category
-->
<logger name="org.springframework">
<level value="info"/>
</logger>
<!--
everything of spring was set to "info" but for class
PropertyEditorRegistrySupport we want "debug" logging
-->
<logger name="org.springframework.beans.PropertyEditorRegistrySupport">
<level value="debug"/>
</logger>
<logger name="org.acegisecurity">
<level value="info"/>
</logger>
<root>
<!--
all log messages of level "debug" or higher will be logged, unless defined otherwise
all log messages will be logged to the appender "stdout", unless defined otherwise
-->
<level value="debug" />
<appender-ref ref="stdout" />
</root>
</log4j:configuration>
TTCC
TTCC는 Log4j가 사용하는 메시지 서식이다.[22] TTCC는 "Time Thread Category Component"를 가리킨다. 다음의 패턴을 사용한다:
%r [%t] %-5p %c %x - %m%n
여기서
출력 예시
467 [main] INFO org.apache.log4j.examples.Sort - Exiting main method.
Remove ads
포팅
Log4Shell 취약점
Log4j 2에서 원격 코드 실행을 수반하는 제로 데이 공격, 즉 서술자 Log4Shell(CVE-2021-44228)이 알리바바그룹에 의해 2021년 11월 24일 발견되어 아파치에 보고되었으며 2021년 12월 9일 트위터를 통해 일반에 공개되었다.[9] 영향을 받는 서비스에는 클라우드플레어, 아이클라우드, 마인크래프트: 자바 에디션,[25] 스팀, 텐센트 QQ, 트위터가 포함된다.[26][27][28]
각주
외부 링크
Wikiwand - on
Seamless Wikipedia browsing. On steroids.
Remove ads