我的java.util.logging.FileHandler.limit属性有问题,因为文件大小超出了限制大小
这是我的应用程序中使用的属性

java.util.logging.FileHandler.pattern = ATMChannelAdapter%u.log
java.util.logging.FileHandler.limit = 2000000
java.util.logging.FileHandler.count = 10
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter


它工作正常,然后在某个时候应用程序只写入一个文件,没有任何限制,超出配置文件的大小大约丰富了1 GB,并且要恢复到正常配置,我必须重新启动应用程序。

操作系统是Windows Server 2012
Java 7

有人有类似的问题吗?这可能在高负载下发生吗?

提前致谢

最佳答案

某些操作正在重置LogManager属性,或者您正在运行java.util.logging.FileHandler integer overflow prevents file rotation。尝试安装以下格式化程序,以查看防止旋转的情况。

public class FileSimpleFormatter extends SimpleFormatter {

    private static final Field METER;
    private static final Field COUNT;

    static {
        try {
            METER = FileHandler.class.getDeclaredField("meter");
            METER.setAccessible(true);

            COUNT = FileHandler.class.getDeclaredField("count");
            COUNT.setAccessible(true);
        } catch (RuntimeException re) {
            throw re;
        } catch (Exception e) {
            throw new ExceptionInInitializerError(e);
        }
    }

    private volatile FileHandler h;

    @Override
    public String getHead(Handler h) {
        this.h = FileHandler.class.cast(h);
        return super.getHead(h);
    }

    private String check() {
        FileHandler target = h;
        if (target != null) {
            try {
                Object os = METER.get(target);
                if (os != null && os.getClass().getName().endsWith("MeteredStream")) {
                    Field written = os.getClass().getDeclaredField("written");
                    written.setAccessible(true);
                    Number c = Number.class.cast(COUNT.get(target));
                    Number w = Number.class.cast(written.get(os));
                    if (c.intValue() <= 0 || w.intValue() < 0) {
                        return String.format("target=%1$s count=%2$s written=%3$s%n",
                                target, c, w);
                    }
                }
            } catch (IllegalAccessException ex) {
                throw (Error) new IllegalAccessError(ex.getMessage()).initCause(ex);
            } catch (NoSuchFieldException ex) {
                throw (Error) new NoSuchFieldError(ex.getMessage()).initCause(ex);
            }
        }
        return "";
    }

    @Override
    public String format(LogRecord record) {
        return super.format(record).concat(check());
    }
}


然后搜索您的日志文件以查看是否找到了任何内容。

更新:Oracle正在JDK-8059767 FileHandler should allow 'long' limits and handle overflow of MeteredStream.written.下解决此问题

10-07 12:28