2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

logbackメモ

Last updated at Posted at 2018-12-12

ライブラリの追加

build.gradle
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'

Logbackの設定 - logback.xmlを追加する

src/main/resourcesを作成

(Eclipseを使っている前提)

  • New > Source Folder

Folder name: src/main/resources

logback.xmlを作成

image.png

logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE logback>
<configuration>
	<appender name="STDOUT"
		class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<pattern>%msg%n</pattern>
		</encoder>
	</appender>
	<root>
		<appender-ref ref="STDOUT" />
	</root>
</configuration>

public class Library {

	public static void main(String[] args) {
		Logger logger = LoggerFactory.getLogger(Library.class);

		logger.warn("warn message");
		logger.info("info message");
	}
}

Console
warn message
info message

logback.xmlの「%msg%n」が効いていることを確認。

単純にファイルに吐き出すように設定

logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE logback>
<configuration>
	<property name="LOG_DIR" value="./logs" />
	<property name="FILE_PREFIX" value="logback" />

	<timestamp key="time" datePattern="yyyyMMdd_HHmmss" />

	<appender name="FILE" class="ch.qos.logback.core.FileAppender">
		<file>${LOG_DIR}/${FILE_PREFIX}_${time}.log</file>
		<encoder>
			<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
		</encoder>
	</appender>
	<root>
		<appender-ref ref="FILE" />
	</root>
</configuration>

ログファイル数を制限したい

注意
コピペでログファイルを作成すると正常に動作しなくなるので気を付ける

fileNamePatternの"%d"トークンで指定した最小単位で削除されていく
maxHistoryで保持するログファイル数を設定(現在書き込み中のログファイルは数えない)
cleanHistoryOnStartがないと削除されない

毎分ごと削除されていく設定
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE logback>
<configuration>
	<property name="LOG_DIR" value="./logs" />
	<property name="FILE_PREFIX" value="logback" />
	<appender name="ROLLING"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<param name="Append" value="false" />
		<rollingPolicy
			class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

			<fileNamePattern>${LOG_DIR}/${FILE_PREFIX}-%d{yyyyMMddHHmm}.log</fileNamePattern>
			<cleanHistoryOnStart>true</cleanHistoryOnStart>
			<maxHistory>1</maxHistory>
		</rollingPolicy>
		<encoder>
			<pattern>%d{yyyy-MM-dd'T'HH:mm:ss'Z'} - %m%n</pattern>
		</encoder>
	</appender>

	<root>
		<appender-ref ref="ROLLING" />
	</root>
</configuration>

ログファイルのサイズを制限したい

ポイントはfileNamePattern に %i を追加しているところ

logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE logback>
<configuration>
	<property name="LOG_DIR" value="./logs" />
	<property name="FILE_PREFIX" value="logback" />

	<timestamp key="time" datePattern="yyyyMMdd_HHmmss" />

	<appender name="ROLLING"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${LOG_DIR}/${FILE_PREFIX}.log</file>

		<rollingPolicy
			class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>${LOG_DIR}/${FILE_PREFIX}-%d{yyyyMMdd_HH}.%i.log</fileNamePattern>
			<timeBasedFileNamingAndTriggeringPolicy
				class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<!-- or whenever the file size reaches 100MB -->
				<maxFileSize>5KB</maxFileSize>
			</timeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>
		<encoder>
			<pattern>%d{yyyy-MM-dd'T'HH:mm:ss'Z'} - %m%n</pattern>
		</encoder>
	</appender>

	<root>
		<appender-ref ref="ROLLING" />
	</root>
</configuration>
logback.xml


logback.xml


その他

設定状況もログで表示させたい

下記をappenderタグの下に追加

<param name="Append" value="false" />

spring-boot

logback-spring.xml
   <include resource="org/springframework/boot/logging/logback/defaults.xml" />
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?