IT자료실/Java

log4j → log4j2 Migration 작업

Ramda 2024. 5. 8. 13:55

Overview

  • log4j는 2015년에 종료 되었고 보안에 취약한 문제가 발견되어 log4j2로 migration하는 작업이 필요하다.

log4j 란?

  • log4j란 log for java라는 의미이다.
  • 로그문 출력을 다양한 대상으로 할 수 있도록 도와주는 도구이다.
  • log4j의 구조
    • Logger : 로그파일을 작성하는 클래스, Appender에 메세지 전달
    • Appender : 로그출력 위치를 결정 (file, console, DB 등)
    • Layout : Appender로 로그 생성 전에 메세지 형식을 만드는 클래스
  • log4j level
    • TRACE < DEBUG < INFO < WARN < ERROR < FATAL

log4j download 및 방법 차이

  • 우선 아래 사이트에서 log4j 파일을 다운로드 한다.
 

Log4j – Download Apache Log4j™ 2

<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apa

logging.apache.org

  •  
  • 공식 문서를 살펴보면 변경하는 방법은 2가지가 있다.
    1. Bridge를 사용하는 방법 → 코드 변경이 없다고 말하고 있다
    2. 직접 파일을 넣고 코드 변경을 하는 방법
  • 직접 변경하고 확인하는 작업을 진행하기 위해 2번 방법을 선택하겠다

Migration 진행

  • 다운 받은 .jar 파일 중에 필요한 파일은 아래 4가지 파일이다.

  • 기존의 lo4j.jar 파일은 삭제해주고 라이브러리 파일 위치에 위 4개의 파일들을 옮겨 놓는다.
  • 먼저 web.xml을 변경해준다. (주석 처리 부분이 기존)

  • 다음으로 log4j2.xml 파일을 만들어서 작성해준다
  • log4j2는 기존 log4j와 문법이 다르므로 공식 문서를 참고하여 변경하여 준다.
 

Log4j – Migrating from Log4j 1.x to 2.x

<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apa

logging.apache.org

  • xml 예시
//Before
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
  <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
    </layout>
  </appender>
	<Root>
    <priority value ="debug" />
    <appender-ref ref="STDOUT" />
  </Root>
</log4j:configuration>

//After
<Configuration>
  <Appenders>
    <Console name="STDOUT" target="SYSTEM_OUT">
      <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
    </Console>
  </Appenders>
    <Root level="debug">
      <AppenderRef ref="STDOUT"/>
    </Root>
</Configuration>
  • 그 다음 코드들의 import와 클래스 메서드를 변경해준다.

  • 메인 패키지들이 변경된 걸 유의하고 잘 변경한다.
  • tomcat의 변경 사항이 있는지 확인하고 argument 확인도 철저히 한다!! (이 과정을 소홀히 하여 시간 낭비를 많이 했다. )
  • 각각의 코드들을 수정해주면 작업은 완료된 사항이다.

Reference

 

Log4j – Overview

Copyright © 1999-2024 The Apache Software Foundation. All Rights Reserved. Apache Logging, Apache Log4j, Log4j, Apache, the Apache feather logo, and the Apache Logging project logo are trademarks of The Apache Software Foundation.

logging.apache.org