Skip to content
Search

Logback

lastmod 2024-11-04created 2015-09-233 edits

Logback is intended as a successor to the popular log4j project.

logback.xml example

<configuration>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="debug">
        <appender-ref ref="CONSOLE" />
    </root>
</configuration>

Logger used in plcGateway of ddrm:

<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true" scanPeriod="3 seconds">
    <property name="LOGS_ABSOLUTE_PATH" value="../log" />

    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>
                %d{HH:mm:ss.SSS} %-5level %logger{32} - %msg%n
            </pattern>
        </encoder>
    </appender>

    <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOGS_ABSOLUTE_PATH}/plcGateway.log.%d{yyyy-MM-dd}.log</fileNamePattern> <!-- 파일이름 패턴 -->
            <maxHistory>30</maxHistory> <!-- 파일 저장 기간 -->
        </rollingPolicy>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} %-5level %logger{32} - %msg%n</pattern> <!-- 파일에 기록할 로그 포맷 -->
        </encoder>
    </appender>


    <root level="DEBUG">
        <!-- <appender-ref ref="console" /> -->
    </root>

    <logger name="com.mseo" level="debug">
        <appender-ref ref="file" />
    </logger>
</configuration>

How to specify the logback.xml file location

By default, Logback automatically loads the logback.xml file on the classpath. So if the file is on the classpath, it is loaded automatically without any extra configuration. <- Needs verification.

If you want it to read a logback.xml file at a specific location, you can specify the file path with the logback.configurationFile system property:

java -Dlogback.configurationFile=/path/to/your/logback.xml -jar your-app.jar

See also

  • Log4j
  • SLF4J
  • Log Aggregator - A system that collects logs accumulated on multiple machines so they can be analyzed all at once.

Favorite site

author Zer0 sitemap