在 Spring Boot 中配置 log4j2.properties

     2023-02-26     236

关键词:

【中文标题】在 Spring Boot 中配置 log4j2.properties【英文标题】:Configuring log4j2.properties in Spring Boot 【发布时间】:2018-06-12 07:08:39 【问题描述】:

我是 Spring Boot 的新手。我有一个正在运行的 Spring Boot 项目。我想使用log4j2(由于项目限制,我必然会使用log4j2本身)将所有不同级别的日志重定向到一个名为'test.log'的日志文件

问题是 - 尽管包含了所有正确的代码,但我无法在我的 test.log 中记录 INFO 级别的日志(我显然想记录错误和调试级别日志也是如此,但至少第一个 INFO 级别日志应该可以正常工作)

--- 我已排除默认日志记录并在 pom.xml 中包含 log4j2 依赖项:

<!-- Exclude Spring Boot's Default Logging -->
        <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>

        <!-- Add Log4j2 Dependency -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.10.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.10.0</version>
        </dependency> 

--- 我在 application.properties 中包含了 logging.config:

logging.file=logs/test.log
logging.level.*=INFO
logging.config=src/main/resources/log4j2.properties

--- 我的log4j2.properties 如下所示:

#status = error // do i need this actually??
dest = logs/test.log
name = PropertiesConfig

property.filename = logs/test.log

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %dyyyy-MM-dd HH:mm:ss %-5p %c1:%L - %m%n

appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = logs/test.log
appender.rolling.filePattern = logs/test1-%dMM-dd-yy-HH-mm-ss-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d %-5p %-17c2 (%30F:%L) %3x - %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 2
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5

logger.rolling.name = com.thp.clinic.allergiesConditions
logger.rolling.level = info
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile
#rootCategory=INFO,rolling,stdout
#logging.level.root=info

#rootLogger.level = debug  //do i necessarily need root Logger????
#rootLogger.appenderRefs = RollingFile
#rootLogger.appenderRef.stdout.ref = STDOUT

logger.rolling.name=org.hibernate.SQL
logger.rolling.level = debug

--- 我的测试 API 的控制器也有以下测试日志行:

    //Logger logger = LogManager.getLogger //is included       
logger.debug("This is a debug message");
logger.info("This is an info message");
logger.warn("This is a warn message");
logger.error("This is an error message");
logger.fatal("This is a fatal message");

--- 据我了解,我已经在代码中包含了所有必需的内容。但是我正面临这个问题 - 当我点击 API 时,只有休眠调试记录器被添加到 test.log;我包含在控制器中的五个测试记录器(甚至使用的其他信息级别日志)没有登录到 test.log;

控制台如下所示(五个记录器中的两个显示在控制台上,但这里也缺少 INFO 级别记录器):

20:05:42.989 [http-nio-8000-exec-1] ERROR com.test.app.appController - This is an error message
20:05:42.994 [http-nio-8000-exec-1] FATAL com.test.app.appController - This is a fatal message
Hibernate: //used hibernate queries are displayed to console as needed

如果有人能指出我需要改变的地方,那将是很大的帮助 在代码中。因为对log4j2理解不当,我猜 log4j2.properties 中的某些内容需要更改

请帮忙!! 提前致谢

【问题讨论】:

【参考方案1】:

log4j2 从 .properties 文件切换到 yaml 或 xml。您需要创建一个 yaml 或 xml 文件来加载 log4j2。

例子:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Configuration status="WARN">
    <Appenders>
        <Console name="ConsoleAppender" target="SYSTEM_OUT">
            <PatternLayout pattern="%ddd-MMM-yyyy HH:mm:ss.SSS [%-5p] %m%n" />
        </Console>
        <RollingFile name="myapp" fileName="$sys:catalina.base/logs/myapp.out"
            filePattern="$sys:catalina.base/logs/myapp-%dyyyy-MM-dd.log.gz">
            <PatternLayout pattern="%ddd-MMM-yyyy HH:mm:ss.SSS [%-5p] %m%n" />
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="10 MB" />
            </Policies>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="TRACE">
            <AppenderRef ref="ConsoleAppender" level="INFO"/>
            <AppenderRef ref="myapp" level="INFO"/>
        </Root>
    </Loggers>
</Configuration>

【讨论】:

@Issac :xml 文件肯定可以完成这项工作,但由于雇主的限制,我正在寻找基于 .properties 的解决方案。仅供参考 - 从 2.4 版开始,Log4j 现在支持通过属性文件进行配置。参考这个:logging.apache.org/log4j/2.x/manual/configuration.html 谢谢你的更新。查看您的属性文件并查看示例,我相信您确实需要根级别集。

将 log4j2.properties 配置到 Spring Boot 中的问题(使用 gradle)

】将log4j2.properties配置到SpringBoot中的问题(使用gradle)【英文标题】:Problemwithconfiguringlog4j2.propertiesintospringboot(usinggradle)【发布时间】:2019-10-2615:14:29【问题描述】:我在scr/main/resources中添加了一个log4j2.properties文件,但它没有... 查看详情

spring-boot application.properties 文件可以与 log4j2.xml 配置一起工作吗?

】spring-bootapplication.properties文件可以与log4j2.xml配置一起工作吗?【英文标题】:Canspring-bootapplication.propertiesfilewokingwithlog4j2.xmlconfiguration?【发布时间】:2019-08-0518:23:24【问题描述】:我想在application.properties中定义高级文件日志... 查看详情

日志记录:在 Spring Boot 中使用 log4j2.properties 文件实现 Log4j2

】日志记录:在SpringBoot中使用log4j2.properties文件实现Log4j2【英文标题】:Logging:Log4j2Implementationusinglog4j2.propertiesfileinSpringBoot【发布时间】:2019-08-3112:03:18【问题描述】:如果我指定,日志记录工作正常\'logging.config=src/main/resources/... 查看详情

如何在 Spring Boot 中获取 log4j2.properties 文件以从 pom.xml 读取文件名

】如何在SpringBoot中获取log4j2.properties文件以从pom.xml读取文件名【英文标题】:Howtogetlog4j2.propertiesfiletoreadfilenamefrompom.xmlinSpringBoot【发布时间】:2018-07-2614:10:06【问题描述】:我一直在网上搜索,我可能错过了。但我想在我的代... 查看详情

spring-boot配置log4j日志

springboot默认使用logback日志记录工具,修改为log4j:<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>1.0.2.RELEASE&l 查看详情

Spring Boot 加载不同名称的 log4j2.xml log4j2-app.xml

】SpringBoot加载不同名称的log4j2.xmllog4j2-app.xml【英文标题】:Springbootloadinglog4j2.xmlwithdifferentNamelog4j2-app.xml【发布时间】:2017-11-1417:46:10【问题描述】:我需要在一个weblogic服务器中部署两个不同的springboot应用程序,我们在两个应... 查看详情

spring-boot - 外部 log4j 配置不起作用

】spring-boot-外部log4j配置不起作用【英文标题】:spring-boot-Externallog4jconfigurationnotworking【发布时间】:2017-05-0505:04:24【问题描述】:我正在尝试为生产环境进行设置,所以我想将log4j.properties保留在我的应用程序jar文件之外。为此... 查看详情

Spring Boot 日志记录属性与 Log4J 2 属性

】SpringBoot日志记录属性与Log4J2属性【英文标题】:SpringBootLoggingPropertiesvsLog4J2Properties【发布时间】:2016-07-1907:51:16【问题描述】:在SpringBootWeb应用程序中,我在application.properties中有以下日志记录属性。logging.level.guru.springframewor... 查看详情

如何在gradle中排除来自spring boot的依赖项

】如何在gradle中排除来自springboot的依赖项【英文标题】:Howtoexcludeadependencycomingfromspringbootingradle【发布时间】:2022-01-1716:30:20【问题描述】:我的项目有springboot2.3.0.RELEASE依赖项,因此我们有某些log4j依赖项,例如org.apache.logging.... 查看详情

如何在spring boot 2中配置netty

】如何在springboot2中配置netty【英文标题】:Howtoconfigurenettyinspringboot2【发布时间】:2018-03-1600:31:20【问题描述】:默认情况下,springwebFlux使用netty,它是单线程事件循环。如何配置springboot,以便为每个核心创建一个线程。谢谢... 查看详情

如何在 Spring boot 2 + Webflux + Thymeleaf 中配置 i18n?

】如何在Springboot2+Webflux+Thymeleaf中配置i18n?【英文标题】:Howtoconfigurei18ninSpringboot2+Webflux+Thymeleaf?【发布时间】:2018-05-1116:04:38【问题描述】:我刚开始一个基于Springboot2+Webflux的新项目。在升级版本的springboot并将spring-boot-starter-... 查看详情

如何在 spring-boot-2 中添加 WebRequestTraceFilter?

】如何在spring-boot-2中添加WebRequestTraceFilter?【英文标题】:HowtoaddWebRequestTraceFilterinspring-boot-2?【发布时间】:2019-01-0204:48:24【问题描述】:我以前有以下配置,但从spring-boot-2.0开始,WebRequestTraceFilter不再可用。@BeanpublicFilterRegist... 查看详情

在 Spring Boot 应用程序中,Hibernate SQL 语句没有使用 log4j 登录到单独的日志文件中

】在SpringBoot应用程序中,HibernateSQL语句没有使用log4j登录到单独的日志文件中【英文标题】:HibernateSQLstatementsarenotgettingloggedinseparatelogfileusinglog4jinspringbootapplication【发布时间】:2020-06-0717:57:12【问题描述】:我在JBOSS中部署了Spr... 查看详情

如何使用 Spring Boot 应用程序初始化 log4j?

】如何使用SpringBoot应用程序初始化log4j?【英文标题】:Howtoinitializelog4jwithSpringBootapplication?【发布时间】:2019-11-1506:57:46【问题描述】:我有一个SpringBoot应用程序,我想在这个应用程序中使用log4j。问题是我有一个JDBC附加程序... 查看详情

springboot2+gradle5+log4j2启动出现stackoverflowerror问题的解决方法

参考技术A在build.gradle中配置好log4j2后,bootRun启动项目,抛出以下异常:原因是新引入的log4j2与spring-boot-starter-logging模块中的log4j冲突了:解决:在build.gradle中,排除spring-boot-starter-logging即可: 查看详情

如何在 Spring-Boot 2 中禁用安全性? [复制]

】如何在Spring-Boot2中禁用安全性?[复制]【英文标题】:HowtodisablesecurityinSpring-Boot2?[duplicate]【发布时间】:2019-01-0306:44:00【问题描述】:在spring-boot-1.x中,我有以下配置来禁用开发模式下的基本安全性:application.properties:security.b... 查看详情

springboot配置log4j2.xml

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-log4j2</artifactId><version>RELEASE</version></dependency><!-- 查看详情

如何覆盖spring boot BOM的版本

】如何覆盖springbootBOM的版本【英文标题】:HowtooverwriteversionofspringbootBOM【发布时间】:2022-01-1914:38:32【问题描述】:我在更新Log4j版本时遇到问题。在我的pom.xml文件中,我更新了这样的版本:<properties>//otherstuff//<org.apache.... 查看详情