0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【logbackの設定】

Posted at

ファイル名は「logback.xml」か、「logback-spring.xml」にする。
ファイルを置く場所は、application.properties(src-main-resoures)のところに書く。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

  <!-- コンソール出力 -->
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <target>System.out</target>
    <encoder>
      <charset>UTF-8</charset>
      <pattern>%d{yyyy/MM/dd HH:mm:ss} %-5level [%thread] - %msg%n</pattern>
    </encoder>
  </appender>

  <!-- ファイル出力 -->
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">

    <!-- コンテナ内の /app/logs/app.log に書き込む -->
    <file>/app/logs/app.log</file>

    <!-- ログファイルのローテーション設定 -->
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>/app/logs/app-%d{yyyy-MM-dd}.log.zip</fileNamePattern>
     <maxHistory>7</maxHistory><!--最大7日保存(8日以降は削除) -->
    </rollingPolicy>

    <!-- ログ出力フォーマット設定 -->
    <encoder>
      <charset>UTF-8</charset>
      <pattern>%d{yyyy/MM/dd HH:mm:ss} %-5level [%logger{36}] - %msg%n</pattern>
    </encoder>
  </appender>

  <!-- すべてのログをコンソールとファイル両方に出す -->
  <root level="INFO">
    <appender-ref ref="STDOUT" />
    <appender-ref ref="FILE" />
  </root>
</configuration>
@RestController
public class TestController {
	
    private final Logger logger = LoggerFactory.getLogger(hreadsController.class);
   
	@GetMapping("/threads")
	public List <ThreadListDto> getThreads() {
        logger.info("ーーーログのテストーーー");
        return threadsService.getThreads();
	}
}

使いたいところのファイルにprivate Logger~の部分を足す。getLogger()でカッコの中身のログを作るって意味

・fileNamePattern
どんな名前で保存するか。

・%d{yyyy-MM-dd}
年、月、日で区切る

・root
ログ内で共通の設定

・ログレベル
ログレベルはログの重要度を表す。
重要度の高い順に、下記の種類がありる。
高い error>warn>info>debug>trace 低い

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?