SpringBootのLogbackの設定方法は何ですか?

Spring Bootでは、ログフレームワークとしてLogbackを使用し、設定ファイルを使ってLogbackを設定することができます。以下はLogbackの設定方法です:

  1. ポムXML
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
</dependency>
  1. ロクバックスプリング.xml
<configuration>
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d %-5level [%thread] %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
    
    <root level="info">
        <appender-ref ref="console" />
    </root>
</configuration>

上記の設定ファイルでは、フォーマット指定されたログの出力先である”console”という名前のAppenderが定義されています。そして、”console” Appenderをroot Loggerに追加し、ログレベルをinfoに設定します。

  1. アプリケーションの設定ファイル
  2. アプリケーションのプロパティ
  3. 警告する (Keikoku suru)
logging.level.root=warn

これで、Logbackの設定が完了しました。アプリケーション内では、Spring Bootが提供するorg.slf4j.Loggerインターフェースを使用してログを記録することができます。例えば:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
public class MyService {
    private static final Logger logger = LoggerFactory.getLogger(MyService.class);
 
    public void doSomething() {
        logger.info("Doing something");
    }
}
bannerAds