码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Boot log4j2 configuration example

时间:2019-07-06 19:10:13      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:default   app   name   public   ack   ogg   you   target   reporting   

 

spring boot 默认log用的是 LogBack。

1. Log4j2 Maven Dependency

pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.2.RELEASE</version>
</parent>
 
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>
 
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
</dependencies>

 2. Add log4j2.xml file in resources folder

如果存在log4j2 jar包,springboot会自动配置它。把 log4j2.xml放在 src/main/resources 文件夹。

 

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
	<Properties>
		<Property name="LOG_PATTERN">%d{yyyy-MM-dd‘T‘HH:mm:ss.SSSZ} %p %m%n</Property>
		<Property name="APP_LOG_ROOT">c:/temp</Property>
	</Properties>
	<Appenders>
		<Console name="Console" target="SYSTEM_OUT" follow="true">
			<PatternLayout pattern="${LOG_PATTERN}" />
		</Console>

		<RollingFile name="appLog"
			fileName="${APP_LOG_ROOT}/SpringBoot2App/application.log"
			filePattern="${APP_LOG_ROOT}/SpringBoot2App/application-%d{yyyy-MM-dd}-%i.log">
			<PatternLayout pattern="${LOG_PATTERN}" />
			<Policies>
				<SizeBasedTriggeringPolicy size="19500KB" />
			</Policies>
			<DefaultRolloverStrategy max="1" />
		</RollingFile>

	</Appenders>
	<Loggers>

		<Logger name="com.howtodoinjava.app" additivity="false">
			<AppenderRef ref="appLog" />
			<AppenderRef ref="Console" />
		</Logger>

		<Root level="debug">
			<AppenderRef ref="Console" />
		</Root>
	</Loggers>
</Configuration>

 3. Spring boot log4j2 demo

Application.java

 

package com.howtodoinjava.app;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
 
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
 
    private static final Logger LOGGER = LogManager.getLogger(Application.class);
 
    public static void main(String[] args)
    {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
         
        LOGGER.info("Info level log message");
        LOGGER.debug("Debug level log message");
        LOGGER.error("Error level log message");
    }
}

 

 

Read More: Log4j2 properties file example

Spring Boot log4j2 configuration example

标签:default   app   name   public   ack   ogg   you   target   reporting   

原文地址:https://www.cnblogs.com/chenqr/p/11143572.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!