Lin-JN 发表于 2023-4-20 07:48:39

干货: 论初始化log4j和自动备份log文件记录的正确姿势

本帖最后由 Lin-JN 于 2023-4-20 07:58 编辑

查看了很多版本的代码,发现都存在同一个毛病,就是代码很乱,而且逻辑并不清晰,全都存在同一个无伤大雅的问题。
总的来说,就是不太严谨吧。


对于不讲究的人来说没啥毛病,但对于强迫症患者,这绝不能忍!
解决方案如下:

首先 做一个log4j初始化参数

import org.apache.log4j.PropertyConfigurator;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/** log4j.properties 初始化配置文件 */
public class initLogRecord {

    private static final String _log_4j = "./config/log4j.properties";

    /** 初始化 log4j 配置文件位置 */
    public static void initLog4j() {
      FileInputStream fileInputStream = null;
      try {
            Properties properties = new Properties();
            fileInputStream = new FileInputStream(_log_4j);
            properties.load(fileInputStream);
            PropertyConfigurator.configure(properties);
      } catch (Exception e) {
            e.printStackTrace();
      } finally {
            if (fileInputStream != null) {
                try {
                  fileInputStream.close();
                } catch (IOException e) {
                  e.printStackTrace();
                }
            }
      }
    }
}然后 修改核心的启动文件 Server.java

    private static final String _logInfo = "./logInfo";
    private static final String _back = "./back";
CompressFile bean = new CompressFile();
      File readfile;
      try {
            File file = new File(_back);
            if (!file.exists()) {
                if (file.mkdir()) {
                  System.err.println(_back + " 文件目录不存在, 已创建目录");
                } else {
                  System.err.println(_back + " 文件目录创建失败");
                }
            }

            File logInfo_file = new File(_logInfo);
            if (!logInfo_file.exists()) {
                if (logInfo_file.mkdir()) {
                  System.err.println(_logInfo + " 文件目录不存在, 已创建目录");
                } else {
                  System.err.println(_logInfo + " 文件目录创建失败,请检查磁盘读写权限");
                }
            }
            String nowDate = new SimpleDateFormat(_time).format(new Date());
            // 不管是否存在记录档案 直接打包文件夹了事
            bean.zip(_logInfo, _back + "/" + nowDate + ".zip");

            String[] logInfo_fileList = logInfo_file.list();
            if (logInfo_fileList != null) {
                for (String fileName : logInfo_fileList) {
                  readfile = new File(_logInfo + "/" + fileName);
                  if (readfile.exists()) { // 文件存在
                        if (readfile.isDirectory()) { // 如果是文件夹目录
                            System.out.println(readfile + " 是文件夹目录,对该目录不做任何处理");
                            continue;
                        }
                        if (readfile.delete()) {
                            System.out.println(readfile + " 记录文件删除成功,稍后初log4j始化会自动创建");
                        } else {
                            System.err.println(readfile + " 文件删除失败,请检查磁盘读写权限");
                        }
                  }
                }
            } else {
                System.err.println(_logInfo + " 文件目录下没有文件,稍后初始化log4j配置文件自动创建");
            }
      } catch (IOException e) {
            System.err.println("log4j 相关文件目录处理失败,请检查磁盘读写权限或文件名格式是否正式");
      } finally { // 无论是否中断 finally 块的语句都会执行
            initLogRecord.initLog4j(); // 初始化 log4j 配置文件位置
      }文件名格式: 一般出错,极大可能是压缩包创建时的文件名格式出错 必须是对应系统所能识别的格式

这段放在 main 方法最前面即可

广告: 圣子默默的个人空间_哔哩哔哩_bilibili

dk52 发表于 2023-4-29 13:03:29

66666666666666666666

15751701111 发表于 2023-12-25 12:51:08

看不懂 好东西

0350tang 发表于 2024-1-15 09:32:39

活捉大佬,{:12:}

rumengtingxue 发表于 2024-1-23 17:31:39

谢谢大鹅的教学,mark一下
import org.apache.log4j.PropertyConfigurator;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/** log4j.properties 初始化配置文件 */
public class initLogRecord {

    private static final String _log_4j = "./config/log4j.properties";

    /** 初始化 log4j 配置文件位置 */
    public static void initLog4j() {
      FileInputStream fileInputStream = null;
      try {
            Properties properties = new Properties();
            fileInputStream = new FileInputStream(_log_4j);
            properties.load(fileInputStream);
            PropertyConfigurator.configure(properties);
      } catch (Exception e) {
            e.printStackTrace();
      } finally {
            if (fileInputStream != null) {
                try {
                  fileInputStream.close();
                } catch (IOException e) {
                  e.printStackTrace();
                }
            }
      }
    }
}
复制代码
然后 修改核心的启动文件 Server.java

    private static final String _logInfo = "./logInfo";
    private static final String _back = "./back";
复制代码
CompressFile bean = new CompressFile();
      File readfile;
      try {
            File file = new File(_back);
            if (!file.exists()) {
                if (file.mkdir()) {
                  System.err.println(_back + " 文件目录不存在, 已创建目录");
                } else {
                  System.err.println(_back + " 文件目录创建失败");
                }
            }

            File logInfo_file = new File(_logInfo);
            if (!logInfo_file.exists()) {
                if (logInfo_file.mkdir()) {
                  System.err.println(_logInfo + " 文件目录不存在, 已创建目录");
                } else {
                  System.err.println(_logInfo + " 文件目录创建失败,请检查磁盘读写权限");
                }
            }
            String nowDate = new SimpleDateFormat(_time).format(new Date());
            // 不管是否存在记录档案 直接打包文件夹了事
            bean.zip(_logInfo, _back + "/" + nowDate + ".zip");

            String[] logInfo_fileList = logInfo_file.list();
            if (logInfo_fileList != null) {
                for (String fileName : logInfo_fileList) {
                  readfile = new File(_logInfo + "/" + fileName);
                  if (readfile.exists()) { // 文件存在
                        if (readfile.isDirectory()) { // 如果是文件夹目录
                            System.out.println(readfile + " 是文件夹目录,对该目录不做任何处理");
                            continue;
                        }
                        if (readfile.delete()) {
                            System.out.println(readfile + " 记录文件删除成功,稍后初log4j始化会自动创建");
                        } else {
                            System.err.println(readfile + " 文件删除失败,请检查磁盘读写权限");
                        }
                  }
                }
            } else {
                System.err.println(_logInfo + " 文件目录下没有文件,稍后初始化log4j配置文件自动创建");
            }
      } catch (IOException e) {
            System.err.println("log4j 相关文件目录处理失败,请检查磁盘读写权限或文件名格式是否正式");
      } finally { // 无论是否中断 finally 块的语句都会执行
            initLogRecord.initLog4j(); // 初始化 log4j 配置文件位置
      }
复制代码
文件名格式: 一般出错,极大可能是压缩包创建时的文件名格式出错 必须是对应系统所能识别的格式

这段放在 main 方法最前面即可

广告: 圣子默默的个人空间_哔哩哔哩_bilibili

页: [1]
查看完整版本: 干货: 论初始化log4j和自动备份log文件记录的正确姿势