简体   繁体   English

使用Logback在每个日志文件的开头添加一个表达式

[英]Add an expression at the start of every log file using Logback

I have a project which has multiple modules. 我有一个包含多个模块的项目。 and every module has its own logback.xml. 每个模块都有自己的logback.xml。 Their appender is rolling files. 他们的附加程序是滚动文件。 How can I add a specific expression at the start of every output log file? 如何在每个输出日志文件的开头添加特定的表达式?

You can add a header to every file created by a (Rolling)FileAppender . 您可以将标题添加到(Rolling)FileAppender创建的每个文件中。

The following configuration will achieve this: 以下配置将实现此目的:

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>target/test.log</file>
    <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <fileHeader>Your File Header ...</fileHeader>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </layout>
    </encoder>
</appender>

With the above configuration the following text will appear before the first line written to the given file by any instance of the Logback subsystem: 通过上述配置,Logback子系统的任何实例在写入给定文件的第一行之前,将出现以下文本:

Your File Header ...

Note: if your application is restarted (or Logback is re initialised within your application) then the PatternLayout will write that header again so you could end up with a log file having the following content: 注意:如果重新启动您的应用程序(或在应用程序内重新初始化了Logback),则PatternLayout将再次写入该标头,因此您可能最终获得一个具有以下内容的日志文件:

Your File Header ...
11:11:59.589 [main] INFO  o.g.sandbox.logback.LogbackTest - hello!
Your File Header ...
11:12:12.352 [main] INFO  o.g.sandbox.logback.LogbackTest - hello!

To avoid this you could write your own Layout or Appender. 为了避免这种情况,您可以编写自己的Layout或Appender。 Here's a simple example extending FileAppender : 这是扩展FileAppender的简单示例:

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();
        }
    }
}

You would configure this like so: 您可以这样配置:

<appender name="FILE" class="org.glytching.sandbox.logging.FileAppenderWithHeader">
    <file>target/test.log</file>
    <header>Your File Header ...</header>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
        </pattern>
    </encoder>
</appender>

And every file created by this appender would start with this line (regardless of how many times the appender is started/stopped): 并且此附加程序创建的每个文件都将从此行开始(无论附加程序启动/停止了多少次):

Your File Header ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM