vertx 3.x에서 logback 사용하기
프로그래밍 2016. 10. 24. 10:20
vertx에서 기본으로 제공하는 로그 설정만으로는 파일 사이즈 제한, 파일 이름에 날짜 넣기 등이 어렵다.
logback을 사용해, 파일 사이즈 제한, 파일 자동 삭제, 파일 이름에 날짜 넣기가 가능하다.
pom.xml에 slf4j와 logback 넣기
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
<dependency> | |
<groupId>org.slf4j</groupId> | |
<artifactId>slf4j-api</artifactId> | |
<version>1.7.21</version> | |
</dependency> | |
<dependency> | |
<groupId>ch.qos.logback</groupId> | |
<artifactId>logback-classic</artifactId> | |
<version>1.1.7</version> | |
<exclusions> | |
<exclusion> | |
<groupId>org.slf4j</groupId> | |
<artifactId>slf4j-api</artifactId> | |
</exclusion> | |
</exclusions> | |
<scope>runtime</scope> | |
</dependency> | |
... |
logback.xml에 로그 설정하기
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<configuration debug="false"> | |
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | |
<!-- On Windows machines setting withJansi to true enables ANSI | |
color code interpretation by the Jansi library. This requires | |
org.fusesource.jansi:jansi:1.8 on the class path. Note that | |
Unix-based operating systems such as Linux and Mac OS X | |
support ANSI color codes by default. --> | |
<withJansi>false</withJansi> | |
<encoder> | |
<pattern>[%d{HH:mm:ss}] %msg%n</pattern> | |
</encoder> | |
</appender> | |
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> | |
<file>logs/app.log</file> | |
<append>true</append> | |
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> | |
<!-- 파일이 하루에 한개씩 생성된다 --> | |
<fileNamePattern>logs/app.%d{yyyy-MM-dd}.%i.log</fileNamePattern> | |
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> | |
<maxFileSize>10MB</maxFileSize> | |
</timeBasedFileNamingAndTriggeringPolicy> | |
<!-- 30일 지난 파일 삭제 --> | |
<maxHistory>30</maxHistory> | |
</rollingPolicy> | |
<encoder> | |
<pattern>[%d{HH:mm:ss}] %msg%n</pattern> | |
</encoder> | |
</appender> | |
<root level="INFO"> | |
<appender-ref ref="STDOUT" /> | |
<appender-ref ref="FILE" /> | |
</root> | |
</configuration> |
Java VM argument에 넣기
-Dvertx.logger-delegate-factory-class-name=io.vertx.core.logging.SLF4JLogDelegateFactory
샘플
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static Logger logger = LoggerFactory.getLogger("io.vertx.core.logging.SLF4JLogDelegateFactory"); | |
logger.info("hello"); | |
[11:00:00] hello |