log4j(日志的配置)

日志一般配置在resources的config下面的,并且Util当中的initLogRecord中的initLog()方法就是加载这个log4j.properties的.

首先先看log4j.properties的配置文件

复制代码
log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=5

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

然后看Util包当中的initLogRecord代码

java 复制代码
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/config/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();
                }
            }
        }
    }
}

下面找个例子测试一下这个日志 当中initLogRecord.initLog();就是查看日志的

java 复制代码
/**
     * 查找所有数据 和日志
     * @throws IOException
     */
    @Test
    public void findAll() throws IOException {
        initLogRecord.initLog();
        //1.读取mybatis的核心配置文件
        InputStream in = Resources.getResourceAsStream("config/mybatis-config.xml");
        //2.通过配置信息获取一个SqlSessionFactory
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(in);
        //3.通过工厂获取一个sqlSession对象
        SqlSession sqlSession = build.openSession();
        //4.通过空间名+id找到要执行的sql语句并执行sql语句
        List<students> list = sqlSession.selectList("studentsMapper.findAll");
        for (students students : list) {
            System.out.println(students);
        }
    }

结果展示

最后切记,一定要在maven的配置当中导入log4j坐标

复制代码
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
相关推荐
wb043072012 天前
厨房质检员——从阿明的“祖传配方“到标准化质检,看测试金字塔的落地
架构·log4j
老码观察7 天前
设计模式实战解读(四):观察者模式——事件驱动的解耦利器
观察者模式·设计模式·log4j
TheRouter9 天前
LLM 应用的Evals 工程实践:从手动测试到自动化回归测试体系
运维·ai·自动化·log4j
老码观察9 天前
设计模式实战解读(二):工厂模式——对象创建的解耦艺术
设计模式·log4j
XiYang-DING10 天前
【Spring】SpringIoC&DI
java·spring·log4j
努力成为AK大王12 天前
超全 Maven 核心知识点总结
log4j
voyaqi15 天前
从零设计企业级校验框架:Spring Boot + SPI 实战指南
spring boot·后端·log4j
前端若水18 天前
智能体开发与传统软件开发的核心区别
网络·人工智能·python·开源·log4j
zlpzlpzyd20 天前
slf4j中jcl-over-slf4j、jul-to-slf4j、log4j-over-slf4j、slf4j-api的区别是什么
java·开发语言·log4j
计算机安禾21 天前
【c++面向对象编程】第9篇:友元(friend):破坏封装的“特权”——真的有害吗?
java·c++·log4j