【问题标题】:Log4j logs show on console without log4j.propertiesLog4j 日志在没有 log4j.properties 的控制台上显示
【发布时间】:2018-03-20 21:31:25
【问题描述】:

我一直在使用 Maven 开发一个 Spring Boot 项目。我需要为系统活动编写日志。我一直在关注这个 log4j 的例子 Example

这是代码:

import org.apache.log4j.Logger;

public class HelloExample{

        final static Logger logger = Logger.getLogger(HelloExample.class);

        public static void main(String[] args) {

            HelloExample obj = new HelloExample();
            obj.runMe("mkyong");

        }

        private void runMe(String parameter){

            if(logger.isDebugEnabled()){
                logger.debug("This is debug : " + parameter);
            }

            if(logger.isInfoEnabled()){
                logger.info("This is info : " + parameter);
            }

            logger.warn("This is warn : " + parameter);
            logger.error("This is error : " + parameter);
            logger.fatal("This is fatal : " + parameter);

        }

    }

我面临两个问题: 1. 主程序编译、运行并给出如下输出,如图所示,没有 log4j.properties

  1. 即使在资源文件夹中有 log4j.properties 文件后,日志也不会存储到日志文件中。

如果应用程序不是springboot,则创建该文件。我不明白我哪里错了。 P.S:我是 Java 和 Spring 的初学者。

【问题讨论】:

标签: java spring maven logging log4j


【解决方案1】:

使用 log4j 的 spring boot,需要使用 spring-boot-starter-log4j2 依赖。解决方法请点击以下链接。

https://www.callicoder.com/spring-boot-log4j-2-example/

【讨论】:

  • 谢谢。我现在明白,由于 SpringBoot 中的默认日志记录,log4j.properties 文件没有被读取。我不得不在 SpringBoot 中禁用默认日志记录。
【解决方案2】:

我建议你看看official documentation。这解释了日志在 Spring 引导应用程序中的工作原理。

关于file output 的段落显示了如何将日志写入特定文件。

【讨论】:

    猜你喜欢
    • 2020-09-17
    • 2015-08-14
    • 2018-11-16
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多