| 
 | 
 
 
发表于 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 
 
 |   
 
 
 
 |