SpringBoot集成log4j2

一、首先使用maven配置依赖包

  
	    <dependency>  
	        <groupId>org.springframework.bootgroupId>  
	        <artifactId>spring-boot-starterartifactId>
	        
	        <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>

二、. 在src/main/resources下新建一个log4j2.xml文件配置如下


<configuration status="ERROR">
	<Properties>
        <Property name="PATTERN" value="[%d{HH:mm:ss.SSS} %-5level] [%t] %c{3} - %msg%n" />
    	<property name="FILE_PATH" value="/mySoft/logs" />
    	<property name="FILE_SIZE" value="50 MB" />
    Properties>
	<Appenders>

		<Console name="Console" target="SYSTEM_OUT">
			<PatternLayout pattern="${PATTERN}"/>
		Console>
		

		<RollingFile name="ProdFile" 
			fileName="${FILE_PATH}/demo.log"
			filePattern="${FILE_PATH}/demo-%d{yyyy-MM-dd}-%i.log">
			<PatternLayout pattern="${PATTERN}" />
			<Policies>
		        <TimeBasedTriggeringPolicy />
		        <SizeBasedTriggeringPolicy size="${FILE_SIZE}"/>
		    Policies>
			<Filters>
	            <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY" />
	        Filters>
		RollingFile>
	Appenders>

	
	<loggers>
		<root level="error">
			<appender-ref ref="Console" />
			<appender-ref ref="ProdFile" />
		root>
	loggers>

configuration> 

你可能感兴趣的:(Spring)