Spring Boot log4j2 configuration example

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

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

时间: 2024-10-09 22:26:51

Spring Boot log4j2 configuration example的相关文章

Command line is too long. Shorten command line for *** or also for Spring Boot default configuration

错误信息: Command line is too long. Shorten command line for *** or also for Spring Boot default configuration 通常会导致的后果是无法启动项目. 解决办法: 修改项目下 .idea\workspace.xml,找到标签 , 在标签里加一行 <property name="dynamic.classpath" value="true" /> 参考解决办法链

Spring Boot + Log4j2 日志框架配置 (Maven)

参考Spring Boot官方文档 日志部分 Spring Boot默认情况下,当使用"Starters" 使用Logback输出日志, 还包括适当的Logback路由, 确保其他的日志框架(Java Util Logging, Commons Logging, Log4j, SLF4J)都能正常使用 Sping Boot文档的 26.5 Custom Log Configuration 章节,说明了自定义日志配置方法 多样的日志系统可以通过 添加适当的日志框架库到classpath

spring boot log4j2配置

[传送门]:log4j官网配置文件详解 1. 排除 spring boot 自带的  spring-boot-starter-logging 依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <!-- remove log4j --> &l

Spring Boot 使用 Log4j2

Java 中比较常用的日志工具类,有 Log4j.SLF4j.Commons-logging(简称jcl).Logback.Log4j2(Log4j 升级版).Jdk Logging. Spring Boot 默认使用 Logback,但相比较而言,Log4j2 在性能上面会更好. Spring Boot(版本1.5.10.RELEASE)中使用 Log4j2 非常简单,添加如下依赖: <!-- Exclude Spring Boot's Default Logging --> <dep

Spring Boot 探索系列 - 自动化配置篇

26. Logging Prev  Part IV. Spring Boot features  Next 26. Logging Spring Boot uses Commons Logging for all internal logging, but leaves the underlying log implementation open. Default configurations are provided for Java Util Logging,Log4J, Log4J2 an

Questions on Spring Boot – Part 3

What is Hot swapping in spring boot? Reloading the changes without restarting the server is called hot swapping, Modern IDEs (Eclipse, IDEA, etc.) all support hot swapping of bytecode,  so if you make a change that doesn’t affect class or method sign

Spring Boot Security

Spring Boot Security Spring Boot includes an additional set of tools that can make the application development time a little more faster by reducing its restart time. Security https://docs.spring.io/spring-boot/docs/current/reference/html/boot-featur

idea spring boot

新建spring boot A: debug: No active profile set, falling back to default profiles: default B: 点击 eidt configurations>spring boot>application>configuration>environment>environment variables>spring.profiles=test 摘自:https://www.cnblogs.com/ly

从Spring MVC 到 Spring BOOT的简化道路

背景 从Servlet技术到Spring和Spring MVC,开发Web应用变得越来越简捷.但是Spring和Spring MVC的众多配置有时却让人望而却步,相信有过Spring MVC开发经验的朋友能深刻体会到这一痛苦.因为即使是开发一个Hello-World的Web应用,都需要我们在pom文件中导入各种依赖,编写web.xml.spring.xml.springmvc.xml配置文件等. 特别是需要导入大量的jar包依赖时,我们需要在网上查找各种jar包资源,各个jar间可能存在着各种依