---
# System prepended metadata

title: 'Spring Boot Logger '
tags: [SpringBoot]

---

---
title: 'Spring Boot Logger '
disqus: hackmd
---
###### tags: `SpringBoot` 
Spring Boot Logger
===

[TOC]

## 筆記目的

了解如何使用Spring Boot Logger


## 先備條件

需要配置兩個文件

1. applications
2. logback-spring.xml

第一個文件只有一行，第二個文件很複雜，但是複製就好，基本上已經把配置寫好

## 程式碼部分

### 檔案結構架構

![](https://i.imgur.com/TJhgSGu.png)


### applications

```
# To decide which environment to use
# prod or dev
spring.profiles.active=dev
# let the spring boot will shut down when finish
spring.main.web-application-type=none

#log
logging.config=classpath:logback-spring.xml
```

### logback-spring.xml

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

<!-- 参考 https://www.mdeditor.tw/pl/pry6/zh-tw -->
<!-- https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/30975/ -->

<configuration>
    <contextName>logback-spring</contextName>
    <property name="pattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg %n"/>
    <property name="LOG_HOME" value="./logs"/>
    <property name="FILE_NAME" value="AllieGoogleOptimizePerformanceObserveApplication"/>
    <property name="MAX_FILE_SIZE" value="30MB"/>
    <property name="MAX_HISTORY" value="30"/><!-- 單位:天 -->

    <!-- 控制檯輸出 -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${pattern}</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>
    

    <!-- 檔案輸出 -->
    <appender name="Main" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/${FILE_NAME}.%d.%i.log</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <maxHistory>${MAX_HISTORY}</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${pattern}</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>
    
    
 	<!-- ALL/TRACE/DEBUG/INFO/WARN/ERROR/FATAL -->
    <root level="INFO">
       	<appender-ref ref="CONSOLE"/>
        <appender-ref ref="Main"/>
    </root>


</configuration>
```

## 效果

1. 會自動產生一個資料夾名字為log，並且位於

![](https://i.imgur.com/4oHHKi5.png)

2. 其內容為

```java=
2021-10-04 09:26:08.541 [main] INFO  c.e.v.GoogleAccountBudgetContractAnnualConsumptionStatisticsApplication - Starting GoogleAccountBudgetContractAnnualConsumptionStatisticsApplication using Java 1.8.0_301 on DESKTOP-SC0VR8S with PID 2080 (C:\Users\TDG_USER\workspaceforv8\google-account-budget-contract-annual-consumption-statistics\target\classes started by TDG_USER in C:\Users\TDG_USER\workspaceforv8\google-account-budget-contract-annual-consumption-statistics) 
2021-10-04 09:26:08.544 [main] INFO  c.e.v.GoogleAccountBudgetContractAnnualConsumptionStatisticsApplication - The following profiles are active: dev 
2021-10-04 09:26:09.280 [main] INFO  c.e.v.GoogleAccountBudgetContractAnnualConsumptionStatisticsApplication - Started GoogleAccountBudgetContractAnnualConsumptionStatisticsApplication in 1.187 seconds (JVM running for 2.03) 
2021-10-04 09:26:09.282 [main] INFO  c.e.v.GoogleAccountBudgetContractAnnualConsumptionStatisticsApplication - 開始Google FB 年度帳號運算消耗 
2021-10-04 09:26:09.283 [main] INFO  c.e.v.b.service.UpdateSpreadsheetFigureService - step 1 把CID 還有 startdate , enddate 接回來 
2021-10-04 09:26:09.519 [main] WARN  c.g.api.client.util.store.FileDataStoreFactory - unable to change permissions for everybody: C:\Users\TDG_USER\workspaceforv8\google-account-budget-contract-annual-consumption-statistics\tokens 
2021-10-04 09:26:09.520 [main] WARN  c.g.api.client.util.store.FileDataStoreFactory - unable to change permissions for owner: C:\Users\TDG_USER\workspaceforv8\google-account-budget-contract-annual-consumption-statistics\tokens 
2021-10-04 09:26:12.586 [main] INFO  c.e.v.b.service.UpdateSpreadsheetFigureService - step 2 parse Google CID to Long array 
2021-10-04 09:26:14.380 [Gax-1] INFO  com.google.ads.googleads.lib.request.summary - SUCCESS REQUEST SUMMARY. Method: google.ads.googleads.v8.services.GoogleAdsService/Search, Endpoint: googleads.googleapis.com:443, CustomerID: 3704605386, RequestID: jW85RclSEjrmAgw9WG9dGQ, ResponseCode: OK, Fault: null. 
2021-10-04 09:26:14.800 [Gax-8] INFO  com.google.ads.googleads.lib.request.summary - SUCCESS REQUEST SUMMARY. Method: google.ads.googleads.v8.services.GoogleAdsService/Search, Endpoint: googleads.googleapis.com:443, CustomerID: 6731208557, RequestID: WOvLhsfBPT4MSF2LfmcnXA, ResponseCode: OK, Fault: null. 
2021-10-04 09:26:15.542 [Gax-12] INFO  com.google.ads.googleads.lib.request.summary - SUCCESS REQUEST SUMMARY. Method: google.ads.googleads.v8.services.GoogleAdsService/Search, Endpoint: googleads.googleapis.com:443, CustomerID: 6760335214, RequestID: 87qelPSghOujaF-WloIdfw, ResponseCode: OK, Fault: null. 
2021-10-04 09:26:16.066 [Gax-13] INFO  com.google.ads.googleads.lib.request.summary - SUCCESS REQUEST SUMMARY. Method: google.ads.googleads.v8.services.GoogleAdsService/Search, Endpoint: googleads.googleapis.com:443, CustomerID: 8092359339, RequestID: 0yr2fG83_eWp9oK1RrmsDQ, ResponseCode: OK, Fault: null. 
2021-10-04 09:26:17.677 [Gax-20] INFO  com.google.ads.googleads.lib.request.summary - SUCCESS REQUEST SUMMARY. Method: google.ads.googleads.v8.services.GoogleAdsService/Search, Endpoint: googleads.googleapis.com:443, CustomerID: 8258490584, RequestID: 2dpaA-0-lJuiYYAk7v-kNw, ResponseCode: OK, Fault: null. 
2021-10-04 09:26:17.681 [main] INFO  c.e.v.b.service.UpdateSpreadsheetFigureService - step 3 把 目標 Google CID 還有對應的MCC 存成hashmap 
2021-10-04 09:26:17.681 [main] INFO  c.e.v.b.service.UpdateSpreadsheetFigureService - step 4 開始計算 Google Annaul Report Bean 
2021-10-04 09:26:18.317 [Gax-24] INFO  com.google.ads.googleads.lib.request.summary - SUCCESS REQUEST SUMMARY. Method: google.ads.googleads.v8.services.GoogleAdsService/Search, Endpoint: googleads.googleapis.com:443, CustomerID: 3651231947, RequestID: CI6UmklLxu3s4E7sEtw65g, ResponseCode: OK, Fault: null. 
2021-10-04 09:26:18.951 [Gax-28] INFO  com.google.ads.googleads.lib.request.summary - SUCCESS REQUEST SUMMARY. Method: google.ads.googleads.v8.services.GoogleAdsService/Search, Endpoint: googleads.googleapis.com:443, CustomerID: 5435483526, RequestID: kDH9L7hyz3B3kdQriVOUHw, ResponseCode: OK, Fault: null. 
2021-10-04 09:26:19.592 [Gax-31] INFO  com.google.ads.googleads.lib.request.summary - SUCCESS REQUEST SUMMARY. Method: google.ads.googleads.v8.services.GoogleAdsService/Search, Endpoint: googleads.googleapis.com:443, CustomerID: 6033705899, RequestID: _fTuhonxtPZgqa4-LNom6g, ResponseCode: OK, Fault: null. 
2021-10-04 09:26:20.310 [Gax-34] INFO  com.google.ads.googleads.lib.request.summary - SUCCESS REQUEST SUMMARY. Method: google.ads.googleads.v8.services.GoogleAdsService/Search, Endpoint: googleads.googleapis.com:443, CustomerID: 6240126898, RequestID: bbz6Cytz-93QutmYExhXPw, ResponseCode: OK, Fault: null. 
2021-10-04 09:26:21.105 [Gax-40] INFO  com.google.ads.googleads.lib.request.summary - SUCCESS REQUEST SUMMARY. Method: google.ads.googleads.v8.services.GoogleAdsService/Search, Endpoint: googleads.googleapis.com:443, CustomerID: 9646799205, RequestID: IV2VUUbneX6gFkHrnRJ5hw, ResponseCode: OK, Fault: null. 
2021-10-04 09:26:21.751 [Gax-44] INFO  com.google.ads.googleads.lib.request.summary - SUCCESS REQUEST SUMMARY. Method: google.ads.googleads.v8.services.GoogleAdsService/Search, Endpoint: googleads.googleapis.com:443, CustomerID: 9772017439, RequestID: YpVjr9He0lI3_cHLdDlMTg, ResponseCode: OK, Fault: null. 
2021-10-04 09:26:21.757 [main] INFO  c.e.v.b.service.UpdateSpreadsheetFigureService - step 5 開始計算 FB Annaul Report Bean 
2021-10-04 09:26:37.567 [main] INFO  c.e.v.b.service.UpdateSpreadsheetFigureService - step 6 開始填寫 將資料填寫到 spreadsheet上 
2021-10-04 09:26:38.717 [main] INFO  c.e.v.b.service.UpdateSpreadsheetFigureService - 計算完畢 程式自動關閉 

```

## 基本於 java class 內的用法，超基本那種

```java=
package com.example.value;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.example.value.business.service.UpdateSpreadsheetFigureService;
import com.example.value.properties.ServiceProperties;

@SpringBootApplication
public class GoogleAccountBudgetContractAnnualConsumptionStatisticsApplication implements ApplicationRunner{
	private static Logger logger = LoggerFactory.getLogger(GoogleAccountBudgetContractAnnualConsumptionStatisticsApplication.class);
	
	@Autowired
	ServiceProperties servicePreperties;
	@Autowired
	UpdateSpreadsheetFigureService updateSpreadsheetFigureService;
	 
	public static void main(String[] args) {
		SpringApplication.run(GoogleAccountBudgetContractAnnualConsumptionStatisticsApplication.class, args);
  
	}

	@Override
	public void run(ApplicationArguments args) throws Exception {
		// TODO Auto-generated method stub
		logger.info("開始Google FB 年度帳號運算消耗");	
	    updateSpreadsheetFigureService.activateDetectProcess(); 
	     
		
	}

}

```

## 變數用法

```
logger.info("Total get " + aiCampaignList.size() + " 筆 AI Campaign by aIOptimizationService...");
```

