标签: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