当前位置: 首页 > article >正文

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代码

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();就是查看日志的

/**
     * 查找所有数据 和日志
     * @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>


http://www.kler.cn/news/163087.html

相关文章:

  • C语言WFC实现绘制贝塞尔曲线的函数
  • 【交流】PHP生成唯一邀请码
  • 安装node.js并创建第一个vue项目
  • 【EI会议征稿中】2024年第四届人工智能、自动化与高性能计算国际会议(AIAHPC 2024)
  • Navicat 技术指引 | 适用于 GaussDB 分布式的数据迁移工具
  • 华为配置Smart Link主备备份示例
  • vue3递归组件---树形组件
  • 【数据仓库-10】-- 数据仓库、数据湖和湖仓一体对比
  • 剑指 Offer(第2版)面试题 21:调整数组顺序使奇数位于偶数前面
  • 做数据分析为何要学统计学(4)——什么问题适合使用卡方检验?
  • 考研真题数据结构
  • python3安装redis
  • Navicat 技术指引 | 适用于 GaussDB 分布式的模型功能
  • Django的logging-日志模块的简单使用方法
  • 微信小程序网络请求二次封装
  • Java项目开发,业务比较复杂如何减少bug
  • 基于深度学习yolov5实现安全帽人体识别工地安全识别系统-反光衣识别系统
  • ArkTS快速入门
  • CentOS常用基础命令大全(linux命令)2
  • Pycharm Jupyter ModuleNotFoundError 问题解决
  • 【前端】CSS基础(学习笔记)
  • Python合并一个 Excel 里面的多张表
  • 虚幻学习笔记10—C++函数与蓝图的通信
  • django与数据库交互关于当前时间的坑
  • 2023.12.7 关于 MySQL 事务详解
  • C#云LIS系统源码 B/S架构,SaaS模式,可扩展性强
  • 数据结构——二叉树的链式结构
  • pcl-3 pcl结合opencv做svm分类(法向量特征数据)
  • 如何运用gpt改写出高质量的文章 (1)
  • 【计算机网络】应用层电子邮件协议