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>
相关推荐
墨痕诉清风1 天前
java漏洞集合工具(Struts2、Fastjson、Weblogic(xml)、Shiro、Log4j、Jboss、SpringCloud)
xml·java·struts·安全·web安全·spring cloud·log4j
Lisonseekpan1 天前
为什么Spring 推荐使用构造器注入而非@Autowired字段注入?
java·后端·spring·log4j
brave and determined2 天前
CANN训练营 学习(day10)昇腾AI算子ST测试全攻略:从入门到精通
自动化测试·人工智能·log4j·算子·fuzz·测试实战·st测试
記億揺晃着的那天3 天前
MyBatis-Plus 单元测试中 Lambda Mock 的坑与解决
单元测试·log4j·mybatis
m0_740043733 天前
SpringBoot05-配置文件-热加载/日志框架slf4j/接口文档工具Swagger/Knife4j
java·spring boot·后端·log4j
她说彩礼65万5 天前
C# params使用
开发语言·c#·log4j
掘根8 天前
【消息队列项目】公共模块实现
log4j
代码欢乐豆10 天前
软件测试测试题——单元测试
软件测试·log4j
询问QQ:6882388611 天前
Matlab机器人工具箱,欧拉角RPY角位姿变换。 机器人技术基础,位姿变换演示小基于Matl...
log4j