dev-sian / meetings

잡다구리
0 stars 0 forks source link

[logging] log4j2 사용법 #10

Open ParkSunJin opened 2 years ago

ParkSunJin commented 2 years ago

우리 프로젝트는 멀티쓰레드 환경에서 성능이 가장 좋은 log4j2를 사용할거임

의존성 설정

먼저, spring-boot-starter-web에는 logging 모듈이 추가되어 있는데, spring에서 지원하는 기본 로깅 모듈은 logback이기 때문에 의존성을 제거해야함. (제거하지 않고 log4j2의 의존성을 가져오면 에러남)

configurations{
    all{
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
}

그리고 의존성 설정

dependencies {
    // log4j2
    implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.17.0'
    implementation group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.17.0'
    implementation group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-yaml', version: '2.10.3'
}

application.yml 설정

logging:
  config: classpath:log4j2.xml       # 또는 log4j2.yml

log4j2.xml 설정

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">

    <Properties>
        <Property name="logFileName">pyeon-schelin</Property>
        <Property name="consoleLayout">%style{%d{ISO8601}} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable</Property>
        <Property name="fileLayout">%d [%t] %-5level %c(%M:%L) - %m%n</Property>
    </Properties>

    <Appenders>
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="${consoleLayout}" disableAnsi="false" />
        </Console>
        <RollingFile name="file" fileName="logs/${logFileName}.log" filePattern="logs/${logFileName}.%d{yyyy-MM-dd-hh}.log">
            <PatternLayout pattern="${fileLayout}" disableAnsi="false"/>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1" /><!-- 시간별 로그 파일 생성-->
            </Policies>
            <DefaultRolloverStrategy max="5" fileIndex="min" /><!-- 롤링 파일 5개 까지 생성 -->
        </RollingFile>
    </Appenders>

    <Loggers>
        <Root level="info" additivity="false">
            <AppenderRef ref="console" level="info"/>
            <AppenderRef ref="file" level="warn"/>
        </Root>

        <!-- 스프링 프레임워크에서 찍는건 level을 info로 설정 -->
        <logger name="org.springframework" level="info" additivity="false" >
            <AppenderRef ref="console" />
            <AppenderRef ref="file" />
        </logger>

        <!-- rolling file에는 debug, console에는 info 분리하여 처리 가능하다. -->
        <logger name="com.devsian" level="debug" additivity="false" >
            <AppenderRef ref="console" level="debug" />
            <AppenderRef ref="file" level="warn" />
        </logger>

    </Loggers>
</Configuration>

https://huisam.tistory.com/entry/log4j2

log4j2.yml(에러나넴ㅎrollingFile 어쩌구 에러남..)

Configutation:
  name: Default
  status: warn

  Properties:
    Property:
    - name: logFileName
      value: "pyeon-schelin"
    - name: log-path
      value: "logs"
    - name: consoleLayout
      value: "%style{%d{ISO8601}} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable"
    - name: fileLayout
      value: "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"

  Appenders:
    Console:
      name: console
      target: SYSTEM_OUT
      PatternLayout:
        pattern: ${consoleLayout}
        disableAnsi: false
    RollingFile:
      - name: file
        fileName: ${log-path}/${logFileName}.log
        PatternLayout:
          pattern: ${fileLayout}
          disableAnsi: false
        Policies:
          SizeBasedTriggeringPolicy:
            size: 1 KB
        DefaultRollOverStrategy:
          max: 5
  Loggers:
    Root:
      level: info
      AppenderRef:
        - ref: console
        - ref: file

slf4j 사용법

image

ParkSunJin commented 2 years ago

미팅때 정할것