log4j警告之log4j:WARN No appenders could be found for logger(org.apache.ibatis.logging.LogFactory).

MyBatis中使用Log4j报警告信息

在使用Log4j作为打印MaBatis日志信息时,报以下WARN。

log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

如下图所示。

image-20210918152156497

原因分析:

  1. 没有创建log4j.properties文件。

  2. log4j.properties文件创建了,但是创建的位置不正确。

    log4j.properties文件应该创建在src目录下。如果不是创建在src目录下,则需要自己写文件加载流,把配置文件加载进来。如何写加载流呢?请往下看。

package util;

import org.apache.log4j.PropertyConfigurator;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class initLogRecord {
    public static void initLog() {
        FileInputStream fileInputStream = null;
        try {
            Properties properties = new Properties();
            fileInputStream = new FileInputStream("src/main/resources/log4j.properties");
            properties.load(fileInputStream);
            PropertyConfigurator.configure(properties);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

注意:如果是自定义加载jog4j.properties文件流,则需要在程序中调用该方法。

默认会扫描src下的log4j.properties配置文件。所以最简单的解决办法就是把log4j.properties文件移动到src目录下。

image-20210918153200101

更多内容可以访问我的个人博客
如果想获取更多视频资源,可以关注我的B站账号,我会不定期分享编程知识。
我的B站账号

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值