实用程序日志记录不会在旋转时删除旧的日志文件 [英] Util logging doesn't delete old log files on rotation

查看:106
本文介绍了实用程序日志记录不会在旋转时删除旧的日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码配置了我的日志记录.

I used the below code to configure my logging.

public Boolean configureLogPath(String logPath, String level, String logComponents, int logFileSize,
        int logFileCount) {
    int logFileSizeInKbs = logFileSize * 1000;
    Boolean result = false;
    String[] splitComponents = logComponents.split(",");
    for (String component : splitComponents) {
        loggableComponents.add(component);
    }
    switch (level) {
    case "info":
        LOGGER.setLevel(Level.INFO);
        break;
    case "severe":
        LOGGER.setLevel(Level.SEVERE);
        break;
    case "debug":
        LOGGER.setLevel(Level.CONFIG);
        break;
    case "off":
        LOGGER.setLevel(Level.OFF);
        break;
    default:
        LOGGER.setLevel(Level.SEVERE);
    }
    try {
        simpleFormatter = new SimpleFormatter();
        logFileHandler = new FileHandler(logPath, logFileSizeInKbs, logFileCount);
        logFileHandler.setFormatter(simpleFormatter);
        LOGGER.setFilter(filter);

        LOGGER.addHandler(logFileHandler);
        result = true;
    } catch (SecurityException e1) {
        result = false;
        LOGGER.log(Level.SEVERE, "Security exception when reading log file" + e1);
    } catch (IOException e1) {
        result = false;
        LOGGER.log(Level.SEVERE, "IO Exception when reading log file" + e1);
    }
    return result;
}

但是,尽管我在FileHandler中给出了相关参数以旋转日志,但仍然保留了旧的日志文件.这是util日志记录的行为,还是我有什么办法可以在轮换期间删除旧文件?

But though I have given the relevant parameters in the FileHandler for the logs to be rotated, the old log files still remain. Is this a behavior of util logging or is there anything I can do to remove old files during rotation?

推荐答案

这是工具日志记录的行为还是在轮换期间我可以做些什么来删除旧文件?

Is this a behavior of util logging or is there anything I can do to remove old files during rotation?

每个FileHandler 文档 :

Per the FileHandler documentation:

对于一组旋转文件,当每个文件达到给定大小时 限制,它将关闭,旋转出去并打开一个新文件.

For a rotating set of files, as each file reaches a given size limit, it is closed, rotated out, and a new file opened.

如果要删除旋转的文件,则只需不旋转即可.轮换的目的是使您可以读取旧的日志数据.

If you want to delete rotated files then simply don't rotate. The goal of rotation is so that you can read old log data.

如果您确实要旋转并删除旧文件,则可以覆盖

If you really want to rotate and delete old files you can override the FileHandler.setOutputStream method to listen for the rotation and write your own delete method.

private boolean constructed;
@Override
protected synchronized void setOutputStream(OutputStream out) throws SecurityException {
    super.setOutputStream(out);
    if (constructed && Level.OFF.equals(super.getLevel())) { //Rotating...
         deleteRotatedFiles();
    }
    constructed = true;
}

这篇关于实用程序日志记录不会在旋转时删除旧的日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆