📜  在 logback 中创建带有一些初始文本的新日志文件 - 无论代码示例

📅  最后修改于: 2022-03-11 14:58:53.789000             🧑  作者: Mango

代码示例1
public class FileAppenderWithHeader extends FileAppender {

    private String header;

    public String getHeader() {
        return header;
    }

    public void setHeader(String header) {
        this.header = header;
    }

    @Override
    public void openFile(String fileName) throws IOException {
        super.openFile(fileName);
        File activeFile = new File(getFile());
        if (activeFile.exists() && activeFile.isFile() && activeFile.length() == 0) {
            write((header + "\n").getBytes());
        }
    }

    private void write(byte[] byteArray) throws IOException {
        if (byteArray == null || byteArray.length == 0)
            return;

        lock.lock();
        try {
            super.getOutputStream().write(byteArray);
            if (super.isImmediateFlush()) {
                super.getOutputStream().flush();
            }
        } finally {
            lock.unlock();
        }
    }
}