您好,登錄后才能下訂單哦!
前言
日志對一個(gè)系統(tǒng)的重要性不言而喻;日志通常是在排查問題時(shí)給人看,一個(gè)友好的輸出樣式讓人看到后賞心悅目,排查效率通常也會隨之提高;下面為大家共享一下通過設(shè)置logback日志輸出格式,打印出令人欣喜的日志樣式。
一、未指定日志格式,日志輸出
1、代碼實(shí)現(xiàn)
(1)演示日志輸出控制器
/*
* Copyright (c) 2019. zhanghan_java@163.com All Rights Reserved.
* 項(xiàng)目名稱:實(shí)戰(zhàn)SpringBoot
* 類名稱:CheckMobileController.java
* 創(chuàng)建人:張晗
* 聯(lián)系方式:zhanghan_java@163.com
* 開源地址: https://github.com/dangnianchuntian/springboot
* 博客地址: https://zhanghan.blog.csdn.net
*/
package com.zhanghan.zhboot.controller;
import com.mysql.jdbc.StringUtils;
import com.zhanghan.zhboot.controller.request.MobileCheckRequest;
import com.zhanghan.zhboot.properties.MobilePreFixProperties;
import com.zhanghan.zhboot.util.wrapper.WrapMapper;
import com.zhanghan.zhboot.util.wrapper.Wrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@Api(value = "校驗(yàn)手機(jī)號控制器", tags = {"校驗(yàn)手機(jī)號控制器"})
public class CheckMobileController {
private static Logger logger = LoggerFactory.getLogger(CheckMobileController.class);
@Autowired
private MobilePreFixProperties mobilePreFixProperties;
@ApiOperation(value = "優(yōu)雅校驗(yàn)手機(jī)號格式方式", tags = {"校驗(yàn)手機(jī)號控制器"})
@RequestMapping(value = "/good/check/mobile", method = RequestMethod.POST)
public Wrapper goodCheckMobile(@RequestBody @Validated MobileCheckRequest mobileCheckRequest) {
logger.info("good check mobile param {}", mobileCheckRequest.toString());
String countryCode = mobileCheckRequest.getCountryCode();
String proFix = mobilePreFixProperties.getPrefixs().get(countryCode);
if (StringUtils.isNullOrEmpty(proFix)) {
logger.error("good check mobile param is error; param is {}, profix is {}", mobileCheckRequest.toString(), proFix);
return WrapMapper.error("參數(shù)錯(cuò)誤");
}
String mobile = mobileCheckRequest.getMobile();
Boolean isLegal = false;
if (mobile.startsWith(proFix)) {
isLegal = true;
}
Map map = new HashMap();
map.put("mobile", mobile);
map.put("isLegal", isLegal);
map.put("proFix", proFix);
return WrapMapper.ok(map);
}
@ApiOperation(value = "擴(kuò)展性差校驗(yàn)手機(jī)號格式方式", tags = {"校驗(yàn)手機(jī)號控制器"})
@RequestMapping(value = "/bad/check/mobile", method = RequestMethod.POST)
public Wrapper badCheckMobile(@RequestBody MobileCheckRequest mobileCheckRequest) {
logger.info("bad check mobile param {}", mobileCheckRequest.toString());
String countryCode = mobileCheckRequest.getCountryCode();
String proFix = "";
if (countryCode.equals("CN")) {
proFix = "86";
} else if (countryCode.equals("US")) {
proFix = "1";
} else { 無錫看婦科的醫(yī)院 http://www.ytsgfk120.com/
logger.error("bad check mobile param is error; param is {}, profix is {}", mobileCheckRequest.toString(), proFix);
return WrapMapper.error("參數(shù)錯(cuò)誤");
}
String mobile = mobileCheckRequest.getMobile();
Boolean isLegal = false;
if (mobile.startsWith(proFix)) {
isLegal = true;
}
Map map = new HashMap();
map.put("mobile", mobile);
map.put("isLegal", isLegal);
map.put("proFix", proFix);
return WrapMapper.ok(map);
}
}
2、項(xiàng)目部署服務(wù)器后訪問打印日志的效果
二、指定日志格式,日志輸出
1、代碼實(shí)現(xiàn)
(1)演示日志輸出控制器(同上)
(2)在項(xiàng)目的resources目錄下增加logback.xml設(shè)置打印格式,logback.xml內(nèi)容如下:
%d{yyyy-MM-dd HH:mm:ss} %highlight(%-5level) %green([${LOG_HOME},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}]) %magenta(${PID:-}) %white(---) %-20(%yellow([%20.20thread])) %-55(%cyan(%.32logger{30}:%L)) %highlight(- %msg%n)
UTF-8
${LOG_PATH}/${appName}-log-console-%d{yyyy-MM-dd}.%i.log.zip
${maxSaveDays}
${maxFileSize}
%d{yyyy-MM-dd HH:mm:ss} %highlight(%-5level) %green([${LOG_HOME},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}]) %magenta(${PID:-}) %white(---) %-20(%yellow([%20.20thread])) %-55(%cyan(%.32logger{30}:%L)) %highlight(- %msg%n)
UTF-8
${LOG_PATH}/${appName}-log-info-%d{yyyy-MM-dd}.%i.log.zip
${maxSaveDays}
${maxFileSize}
%d{"yyyy-MM-dd HH:mm:ss,SSS"}[%X{userId}|%X{sessionId}][%p][%c{0}-%M]-%m%n
UTF-8
ERROR
DENY
ACCEPT
${LOG_PATH}/${appName}-log-error-%d{yyyy-MM-dd}.%i.log.zip
${maxSaveDays}
${maxFileSize}
%d{"yyyy-MM-dd HH:mm:ss,SSS"}[%X{userId}|%X{sessionId}][%p][%c{0}-%M]-%m%n
UTF-8
ERROR
ACCEPT
DENY
level="INFO"/>
3、項(xiàng)目部署服務(wù)器后訪問打印日志的效果
4、查看日志記錄文件,效果也一樣,效果圖:
三、項(xiàng)目地址
1、地址:https://github.com/dangnianchuntian/springboot
2、代碼版本:1.5.0-Release
【總結(jié)】
1、通過設(shè)定日志格式,輸出的樣式更加人性化,錯(cuò)誤也更加明顯;
2、這個(gè)小小的改變,使得在排查程序時(shí)更加的賞心悅目,心情上的開心將在無形中增加排錯(cuò)的效率。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。