滚动文件实施 [英] Rolling file implementation

查看:115
本文介绍了滚动文件实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直很好奇如何在日志中实现滚动文件.

I am always curious how a rolling file is implemented in logs.

为了确保不超过文件大小,人们甚至将如何开始创建任何语言的文件编写类.

How would one even start creating a file writing class in any language in order to ensure that the file size is not exceeded.

我能想到的唯一可能的解决方案是:

The only possible solution I can think of is this:

write method:
    size = file size + size of string to write
    if(size > limit)
        close the file writer
        open file reader
        read the file
        close file reader
        open file writer (clears the whole file)
        remove the size from the beginning to accommodate for new string to write
        write the new truncated string
    write the string we received

这似乎是一个糟糕的实现,但我想不出更好的方法.

This seems like a terrible implementation, but I can not think up of anything better.

特别是,我很想看到Java中的解决方案.

Specifically I would love to see a solution in java.

通过从开头删除大小,假设我有20个字节的字符串(这是限制),我想再写一个3字节的字符串,因此我从开头删除了3个字节,并剩下结尾17个字节,通过追加新字符串,我有20个字节.

By remove size from the beginning is, let's say I have 20 byte string (which is the limit), I want to write another 3 byte string, therefore I remove 3 bytes from the beginning, and am left with end 17 bytes, and by appending the new string I have 20 bytes.

推荐答案

由于您的问题使我对此感到关注,因此下面是logback日志记录框架中的示例. RollingfileAppender#rollover()方法如下:

Because your question made me look into it, here's an example from the logback logging framework. The RollingfileAppender#rollover() method looks like this:

public void rollover() {
    synchronized (lock) {
        // Note: This method needs to be synchronized because it needs exclusive
        // access while it closes and then re-opens the target file.
        //
        // make sure to close the hereto active log file! Renaming under windows
        // does not work for open files
        this.closeOutputStream();

        try {
            rollingPolicy.rollover(); // this actually does the renaming of files
        } catch (RolloverFailure rf) {
            addWarn("RolloverFailure occurred. Deferring roll-over.");
            // we failed to roll-over, let us not truncate and risk data loss
            this.append = true;
        }

        try {
            // update the currentlyActiveFile           
            currentlyActiveFile = new File(rollingPolicy.getActiveFileName());

            // This will also close the file. This is OK since multiple
            // close operations are safe.
            // COMMENT MINE this also sets the new OutputStream for the new file
            this.openFile(rollingPolicy.getActiveFileName()); 
        } catch (IOException e) {
            addError("setFile(" + fileName + ", false) call failed.", e);
        }
    }
}

如您所见,逻辑与您发布的逻辑非常相似.他们关闭当前的OutputStream,执行过渡,然后打开一个新的(openFile()).显然,这都是在synchronized块中完成的,因为许多线程正在使用记录器,但是一次只能发生一次翻转.

As you can see, the logic is pretty similar to what you posted. They close the current OutputStream, perform the rollover, then open a new one (openFile()). Obviously, this is all done in a synchronized block since many threads are using the logger, but only one rollover should occur at a time.

RollingPolicy是有关如何执行过渡的策略,而TriggeringPolicy是何时进行过渡的策略.使用logback,通常可以将这些策略基于文件大小或时间.

A RollingPolicy is a policy on how to perform a rollover and a TriggeringPolicy is when to perform a rollover. With logback, you usually base these policies on file size or time.

这篇关于滚动文件实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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