LoginSignup
9
10

More than 5 years have passed since last update.

Spring Boot 1.3.0のlogback extensionsについて

Last updated at Posted at 2015-10-02

概要

まだSpring-Boot 1.3.0のGAはリリースされていませんが (2015/10/02時点ではM5)、1.3.0よりlogbackの拡張機能が使用できるようになります。
またlogbackに関する変更点がいくつかあります。

環境

下記の環境で動作確認を行いました。

  • Windows7 (64bit)
  • Spring Boot 1.3.0.M5

参考

変更点

logback-spring.xml

1.3.0.M2よりlogback設定ファイルの名前は、logback-spring.xmlが推奨されます。

When possible we recommend that you use the -spring variants for your logging configuration (for example logback-spring.xml rather than logback.xml). If you use standard configuration locations, Spring cannot completely control log initialization.

Initialization failures

1.3.0.M2よりlogback設定ファイルに不備がありlogbackの初期化に失敗した場合は、アプリケーションの起動に失敗するようになります。

Spring Boot 1.3 fails due to the missing file.

>java -jar -Dspring.profiles.active=dev sblb-example-1.0-SNAPSHOT.jar
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: Logback configuration error detected:

Logback extensions

1.3.0.M2より下記のlogbackの拡張機能が使用できるようになります。

springProfile

<springProfile>タグを使用してプロファイル毎に設定を有効にすることができます。
name属性にプロファイル名を記述します。カンマ区切りで複数のプロファイルを指定することができます。

logback-spring.xml
  <springProfile name="dev">
    <include resource="logback-dev-logger.xml"/>
  </springProfile>

  <springProfile name="production">
    <include resource="logback-prod-logger.xml"/>
  </springProfile>

springProperty

<springProperty>タグを使用してapplication設定ファイルの値を参照することができます。

application.yml
myapp:
  log:
    path: d:/logs
    name: sblg-example.log
logback-spring.xml
  <springProperty scope="context" name="logpath" source="myapp.log.path"/>
  <springProperty scope="context" name="logname" source="myapp.log.name"/>

  <appender name="FILE" class="ch.qos.logback.core.FileAppender">
     <file>${logpath}/${logname}</file>
  </appender>

deprecated Log4j 1.x

logbackとは関係ありませんが、1.3.0.M5よりLog4j 1.xの使用が非推奨になりました。

Log4JLoggingSystem is now deprecated following Apache’s EOL declaration for log4j 1.x.

9
10
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
9
10