Springboot使用log4j2日志框架

文章目录

    • 1.pom.xml引入依赖
    • 2.配置文件引入log4j2的配置文件
    • 3.导入log4j2配置文件
    • 4.通过@Slf4j注解来使用log.info()等
    • 最后

1.pom.xml引入依赖

提示:lombok用于@Slf4j注解

		<dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <exclusions>
                
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-loggingartifactId>
                exclusion>
            exclusions>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-log4j2artifactId>
        dependency>
        
		<dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>

2.配置文件引入log4j2的配置文件

提示:在配置文件yaml中引入

logging:
  config: classpath:log4j2-spring.xml

3.导入log4j2配置文件

提示:文件名为log4j2-spring.xml,放置在/src/main/resources文件夹下与yaml配置文件同路径,可直接使用下面的配置文件信息,或根据自己需求修改。





<configuration status="INFO" monitorInterval="30">
	
	<properties>
		
		
		
		<Property name="log_path">logsProperty>

		
		<property name="KEEP_LOG_DAY">60Dproperty>
		
		<property name="EVERY_FILE_SIZE">5Mproperty>
	properties>
	
	<appenders>
		<console name="Console" target="SYSTEM_OUT">
			
			<PatternLayout 
				pattern="[%d][%t][%p][%c:%L] %m%n" />
			<ThresholdFilter level="info" onMatch="ACCEPT"
				onMismatch="DENY" />
		console>
		
		
		
		
		

		
		<RollingFile name="RollingFileInfo"
			fileName="${log_path}/info.log"
			filePattern="${log_path}/$${date:yyyy-MM-dd}/info-%d{yyyy-MM-dd}-%i.log">
			
			<ThresholdFilter level="info" onMatch="ACCEPT"
				onMismatch="DENY" />
			
			
			
			
			
			<PatternLayout 
				pattern="[%d][%t][%p][%c:%L] %m%n" />
			<Policies>
				
				
				<TimeBasedTriggeringPolicy />
				
				<SizeBasedTriggeringPolicy
					size="${EVERY_FILE_SIZE}" />
			Policies>
			 
			<DefaultRolloverStrategy max="256">
				<Delete basePath="${log_path}/" maxDepth="3">
					<IfFileName glob="*/*info*.log" />
					<IfLastModified age="${KEEP_LOG_DAY}" />
				Delete>
			DefaultRolloverStrategy>
		RollingFile>


		<RollingFile name="RollingFileWarn"
			fileName="${log_path}/warn.log"
			filePattern="${log_path}/$${date:yyyy-MM-dd}/warn-%d{yyyy-MM-dd}-%i.log">
			<ThresholdFilter level="warn" onMatch="ACCEPT"
				onMismatch="DENY" />
			
			<PatternLayout 
				pattern="[%d][%t][%p][%c:%L] %m%n" />
			<Policies>
				<TimeBasedTriggeringPolicy />
				<SizeBasedTriggeringPolicy
					size="${EVERY_FILE_SIZE}" />
			Policies>
			
			<DefaultRolloverStrategy max="256">
				<Delete basePath="${log_path}/" maxDepth="3">
					<IfFileName glob="*/*warn*.log" />
					<IfLastModified age="${KEEP_LOG_DAY}" />
				Delete>
			DefaultRolloverStrategy>
		RollingFile>
		<RollingFile name="RollingFileError"
			fileName="${log_path}/error.log"
			filePattern="${log_path}/$${date:yyyy-MM-dd}/error-%d{yyyy-MM-dd}-%i.log">
			<ThresholdFilter level="error" onMatch="ACCEPT"
				onMismatch="DENY" />
			
			<PatternLayout 
				pattern="[%d][%t][%p][%c:%L] %m%n" />
			<Policies>
				<TimeBasedTriggeringPolicy />
				<SizeBasedTriggeringPolicy
					size="1${EVERY_FILE_SIZE}" />
			Policies>
			
			<DefaultRolloverStrategy max="256">
				<Delete basePath="${log_path}/" maxDepth="3">
					<IfFileName glob="*/*error*.log" />
					<IfLastModified age="180D" />
				Delete>
			DefaultRolloverStrategy>
		RollingFile>
	appenders>
	
	<loggers>
		
		<logger name="org.springframework" level="INFO">logger>
		<logger name="org.mybatis" level="DEBUG">logger>
		
		<root level="ALL">
			<appender-ref ref="Console" />
			<appender-ref ref="RollingFileInfo" />
			<appender-ref ref="RollingFileWarn" />
			<appender-ref ref="RollingFileError" />
		root>
		<logger name="cn.timebusker.util" level="INFO">
			<appender-ref ref="RollingFileInfo" />
		logger>
	loggers>
configuration>

4.通过@Slf4j注解来使用log.info()等

提示:添加@Slf4j注解,然后在下面直接使用log.info()等方法

package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.LoginServcie;
import com.example.demo.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
public class LoginController {

    @PostMapping("/user/login")
    public ResponseResult login(@RequestBody User user) {
        log.info("111-登录成功!");
        return null;
    }
}

控制台打印结果
Springboot使用log4j2日志框架_第1张图片
info.log文件中打印的结果
Springboot使用log4j2日志框架_第2张图片

最后

到此为止,欢迎大家交流!

你可能感兴趣的:(Java,spring,boot,log4j,java)