springboot log4j2
Posted yytxdy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot log4j2相关的知识,希望对你有一定的参考价值。
添加maven依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> </parent> <artifactId>springboot-log4j2</artifactId> <name>springboot-log4j2</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</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-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <dependency> <groupId>com.lmax</groupId> <artifactId>disruptor</artifactId> <version>3.4.2</version> </dependency> </dependencies> </project>
添加application.properties并添加配置
spring.profiles.active=dev
logging.config=classpath:log4j2-spring-${spring.profiles.active}.xml
添加log4j2-spring-dev.xml文件
<?xml version="1.0" encoding="UTF-8"?> <configuration> <Properties> <Property name="filename">/opt/logs/demo-$${sd:type}.log</Property> </Properties> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> </Console> <RollingFile name="RollingFile" fileName="/opt/logs/demo.log" filePattern="/opt/logs/$${date:yyyy-MM}/demo-%d{yyyy-MM-dd}-%i.log.gz"> <PatternLayout> <Pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="6" modulate="true"/> <SizeBasedTriggeringPolicy size="250 MB"/> </Policies> <DefaultRolloverStrategy max="14"/> </RollingFile> </Appenders> <loggers> <AsyncLogger name="com.springboot.demo.log4j2" level="INFO" additivity="false"> <AppenderRef ref="Console"/> <AppenderRef ref="RollingFile"/> </AsyncLogger> <asyncRoot level="ERROR"> <AppenderRef ref="Console"/> <AppenderRef ref="RollingFile"/> </asyncRoot> </loggers> </configuration>
添加demo程序
@SpringBootApplication @RestController public class SpringbootLog4jApplication { private static Logger logger = LoggerFactory.getLogger(SpringbootLog4jApplication.class); @RequestMapping("/") public String index() { logger.info("access to url /index"); return "index"; } public static void main(String[] args) { SpringApplication.run(SpringbootLog4jApplication.class, args); } }
以上是关于springboot log4j2的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot2.0 基础案例(02):配置Log4j2,实现不同环境日志打印
SpringBoot2.0 基础案例(02):配置Log4j2,实现不同环境日志打印