Day 18 - Spring Boot 日志纪录

日志纪录是网站的一个非常重要的功能,不论是对外的使用者或是对内的管理,实际运营上一定都会遇到许许多多的问题,问题发生时我们会去查阅纪录并寻找问题,但当我们的纪录都是由服务器预设的处理方式,想要查阅纪录就宛如大海捞针,所以建置自己的日志纪录就是一个很好的解决方法。
Spring Boot 已经有默认配置了Java Util Loggin、Log4J、Log4J2、Logback 以及SLF4J 等,而本篇使用的是SLF4J。

SLF4J

SLF4J (Simple Loggin Facade For Java),作为一个日志纪录的框架,定义了统一的抽象介面,它的作者也是Log4J 的作者,他宣称SLF4J 比Log4J 更有效率,而且比JCL (Apache Commons Logging) 简单、稳定。
在传统的Log4J 中,日志等级分为六个,根据等级高低依序为TRACE、DEBUG、INFO、WARN、ERROR、FATAL 六个等级,FATAL 为最高等级,而SLF4J 认为ERROR 与FATAL 并没有实质上的差别所以拿掉了。

实作

配置Logging 属性

# 指定Log 档案位置,不指定路径则预设在当前专案下生成档案
logging.file.path=/home/kaijun/Documents/iThomeIronManLog

# 设定Log 档案名称,预设为spring.log,使用该配置会覆盖path 位置,直接在当前专案下生成档案
logging.file.name=iThomeIronManLog.log

#  在控制台输出的日志的格式
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n

# 指定档案中日志输出的格式
logging.pattern.file=%d{yyyy-MM-dd} === [%thread] === %-5level === %logger{50} ==== %msg%n

# 指定自定义 logger 物件日志级别
logging.level.com.codewhite=trace

修改控制器层的登入功能

package com.example.iThomeIronMan.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.iThomeIronMan.model.Member;
import com.example.iThomeIronMan.model.MemberAccount;
import com.example.iThomeIronMan.service.MemberAccountService;

@Controller
public class LoginRegisterController {

	@Autowired
	private MemberAccountService memberAccountService;
	
	private static final Logger logger = LoggerFactory.getLogger(LoginRegisterController.class)

	@RequestMapping(value = "/login", method = {RequestMethod.POST})
	public String doLogin(
			@ModelAttribute MemberAccount memberAccount, 
			Model model) {
		
		Member result = memberAccountService.login(memberAccount);
		if(result == null) {
			logger.warn(memberAccount.getAccount() + "尝试登入系统");
			return "redirect:login";
		}
		logger.info(result.getName() + "登入系统");
		return "redirect:information";
	}
	
	// 以下忽略

}

参考网站

SLF4J - 维基百科,自由的百科全书
SpringBoot从零入门4_日志记录及其配置详解


<<:  Day26-"练习-1"

>>:  Flutter基础介绍与实作-Day19 FireBase-设定问题

Day8 PHP数据类型

在php 中总共有这8种数据类型,接下来我会对数据类型做一个介绍。 String(字符串) Inte...

Day 28: Behavioral patterns - Visitor

目的 当一群相似结构的物件们,在执行相同方法时却有着不同实作内容,那可以将方法封装成独立物件。当需要...

Day9 渲染元件

渲染(render)元件 我们知道了JSX的语法写出元件的结构,也知道如何宣告元件了, 现在来学如何...

Vue3 ( JsES6、this、指令、OptionAPI ) -1

1.v-mould写入、渲染 (1) (2) (3) (4) (5) (6) 重点: 1.阵列 与 ...

RISC-V: R-type 位元运算指令

今天一样是简单的 AND、OR、XOR指令实作, 多出来的时间就来还摸索期留下来的债, 让整个专案变...