수색…


구성 파일

구성

Log4j 구성 파일은 다음 형식 중 하나 일 수 있습니다.

  • JSON
  • YAML
  • 속성 (텍스트 파일)
  • XML

구성 검색

  1. Log4j는 log4j.configurationFile 시스템 등록 정보를 검사하고, 설정되어 있으면 구성로드를 시도합니다.
  2. 시스템 등록 정보가 설정되어 있지 않으면 log4j는 classpath에 log4j2-test.properties 를 찾습니다.
  3. 그런 파일이 없으면 log4j는 classpath에 log4j2-test.yaml 또는 log4j2-test.yml .
  4. 그런 파일이 없으면 log4j는 클래스 경로에 log4j2-test.json 또는 log4j2-test.jsn .
  5. 그런 파일이 없으면 logj4는 클래스 경로에서 log4j2-test.xml 을 찾습니다.
  6. 테스트 파일을 찾을 수 없으면 log4j는 클래스 경로에서 log4j2.properties 를 찾습니다.
  7. 등록 정보 파일을 찾을 수 없으면 log4j는 classpath에 log4j2.yaml 또는 log4j2.yml 을 찾습니다.
  8. YAML 파일을 찾을 수 없으면 log4j는 클래스 경로에서 log4j2.json 또는 log4j2.jsn 을 찾습니다.
  9. JSON 파일을 찾을 수없는 경우 log4j는 classpath에 log4j2.xml 을 찾으려고 시도합니다.
  10. 구성 파일을 찾을 수 없으면 DefaultConfiguration이 사용됩니다. 이로 인해 로깅 출력이 콘솔로 이동합니다.

XML 구성

XML 예제

아래의 구성은 두 개의 애펜더 (로그 출력)를 구성합니다. 첫 번째는 표준 시스템 출력 (콘솔)에 로그하고 다른 로그는 파일에 기록합니다. 이 예제에서 파일의 위치는 구성 (appender file )에서 정적으로 설정하거나 maven 필터링 기능 ( <Property name="APPENDER"> )을 통해 동적으로 설정할 수 있습니다. 로그 인 파일은 하루에 압축됩니다. 로그 라인 형식 Conversion Pattern 은 변수로 설정됩니다.

<?xml version="1.0" encoding="UTF-8"?>
<!-- 'status' sets log level for parsing configuration file itself -->
<Configuration status="INFO">
<Properties>
    <!-- Sets variable PID, if it's not present in log4j context will take value as below: -->
    <Property name="PID">????</Property>
    <!-- Sets variable 'LOG_PATTERN', defining how log line will look like -->
    <Property name="LOG_PATTERN">%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{%X{usr}}{green} %clr{---}{faint}%clr{[%15.15t]}{faint} %clr{%-40.40c{1.}:%l}{cyan} %clr{:}{faint} %m%n%wEx</Property>
    <!-- LOG_DIR may be set by maven filtering feature: -->
    <Property name="LOG_DIR">@logging.path@</Property>
    <!-- APPENDER may be set by maven filtering feature, to set 'console' or 'file' appender: -->
    <Property name="APPENDER">@logging.default.appender@</Property>
</Properties>
<Appenders>
    <!-- Sets console output: -->
    <Console name="console" target="SYSTEM_OUT">
        <PatternLayout pattern="${LOG_PATTERN}"/>
    </Console>
    <!-- Sets output to file, and names it 'file' -->
    <RollingRandomAccessFile append="true" fileName="${LOG_DIR}/log4j2.log"
        filePattern = "${LOG_DIR}/log4j2.%d{yyyy-MM-dd}.nr%i.log.gz" name="file">
        <PatternLayout>
            <Pattern>${LOG_PATTERN}</Pattern>
        </PatternLayout>
        <Policies>
            <TimeBasedTriggeringPolicy />
        </Policies>
    </RollingRandomAccessFile>
</Appenders>
<Loggers>
    <!-- Sets debug for Class 'com.example.package.Clazz', and attaches to file 'file' -->
    <Logger name="com.example.package.Clazz" level="debug">
        <AppenderRef ref="file"/>
    </Logger>
    <!-- Sets 'info' for package 'org.springframework' -->
    <Logger name="org.springframework" level="info" />
    <Root level="WARN">
        <AppenderRef ref="${APPENDER}"/>
    </Root>
</Loggers>
</Configuration>


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow