spring

spring boot fast log4j2

kimbs0301 2024. 10. 12. 10:20

build.gradle

dependencies {
    implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.17.2'
    implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.17.2'
    implementation group: 'org.apache.logging.log4j', name: 'log4j-jul', version: '2.17.2'
    implementation group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.17.2'
    implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.36'
    implementation group: 'org.slf4j', name: 'jul-to-slf4j', version: '1.7.36'
    implementation 'com.lmax:disruptor:3.4.4'

    implementation 'org.springframework.boot:spring-boot-starter-web:2.6.14'

 

application.yml

logging:
  charset:
    console: UTF-8
    file: UTF-8
  level:
    root: info
    com:
      example: info
  file.name: C:/logs/server.log
  pattern.console:
  pattern.file: "%d{HH:mm:ss.SSS} %-5level [%t] %logger{39} : %m%n"
  logback:
    rollingpolicy:
      max-file-size: 10MB
      max-history: 100

 

ApiController.java

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.message.SimpleMessage;
..
public class ApiController {
    private final static org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger("ApiController");
..
String message = "hello";
// logMessage(level, marker, fqcn, location, message, throwable)
log.logMessage(Level.INFO, null , "ApiController", null, new SimpleMessage(message), null);

 

java run

java ... -DLOG_PATH=C:/logs -DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector -jar application.jar

 

스프링 부트 기본 파일 로그 보다 30% 정도의 성능이 향상된다.