更改附加器级别而不是记录器级别

时间:2019-01-01 07:18:38

标签: spring-boot log4j2 spring-boot-admin

我的团队使用Spring Boot Admin来控制我们的spring application
Spring Boot Admin中,我们可以选择更改Runtime中的logger级别,

我们为每个task(线程)都有一个单独的记录器,如果我们只想查看一个线程的控制台日志,我们将关闭所有其他线程记录器, 问题在于,每个记录器都会同时发送STDOUT和特定文件的输出,而我们只想关闭 标准输出。

log4j2.xml配置示例:

<Loggers>
   <Logger name="task1" level="info">
     <AppenderRef ref="Console"/>
     <AppenderRef ref="File"/>
   </Logger>
   <Logger name="task2" level="info">
     <AppenderRef ref="Console"/>
     <AppenderRef ref="File"/>
   </Logger>
</Loggers>

我们尝试了很多解决方案:

  • 结合使用父级记录器和可加性,并将每个追加器分别分配到不同的记录器中, 有任何想法吗?

1 个答案:

答案 0 :(得分:2)

默认情况下,Log4j2不允许管理System.out和System.err流。

要阐明控制台记录器如何工作: 只需Console附加程序即可将其输出打印到System.out或System.err。根据文档,如果默认情况下未指定目标,它将打印到System.out:

https://logging.apache.org/log4j/2.x/manual/appenders.html

  

目标||字符串|| “ SYSTEM_OUT”或“ SYSTEM_ERR”。默认值为“ SYSTEM_OUT”。


以下是示例:

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <Properties>
        <Property name="log-pattern">%d{ISO8601} %-5p %m\n</Property>
    </Properties>
    <appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout>
                <pattern>${log-pattern}</pattern>
            </PatternLayout>
        </Console>
    </appenders>
    <Loggers>
        <logger name="testLogger" level="info" additivity="false">
            <AppenderRef ref="Console"/>
        </logger>
    </Loggers>
</configuration>

LogApp.java

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class LogApp {
    public static void main(String[] args) {
        Logger log = LogManager.getLogger("testLogger");
        log.info("Logger output test!");
        System.out.println("System out test!");
    }
}

输出:

2019-01-08T19:08:57,587 INFO  Logger output test!
System out test!

管理系统流的解决方法

参加Dmitry Pavlenko的流重定向课程

https://sysgears.com/articles/how-to-redirect-stdout-and-stderr-writing-to-a-log4j-appender/

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.io.OutputStream;

/**
  * A change was made on the existing code:
  * - At (LoggingOutputStream#flush) method 'count' could contain 
  *  single space character, this types of logs has been skipped
  */
public class LoggingOutputStream extends OutputStream {
    private static final int DEFAULT_BUFFER_LENGTH = 2048;
    private boolean hasBeenClosed = false;
    private byte[] buf;
    private int count;

    private int curBufLength;

    private Logger log;

    private Level level;

    public LoggingOutputStream(final Logger log,
                               final Level level)
            throws IllegalArgumentException {
        if (log == null || level == null) {
            throw new IllegalArgumentException(
                    "Logger or log level must be not null");
        }
        this.log = log;
        this.level = level;
        curBufLength = DEFAULT_BUFFER_LENGTH;
        buf = new byte[curBufLength];
        count = 0;
    }

    public void write(final int b) throws IOException {
        if (hasBeenClosed) {
            throw new IOException("The stream has been closed.");
        }
        // don't log nulls
        if (b == 0) {
            return;
        }
        // would this be writing past the buffer?
        if (count == curBufLength) {
            // grow the buffer
            final int newBufLength = curBufLength +
                    DEFAULT_BUFFER_LENGTH;
            final byte[] newBuf = new byte[newBufLength];
            System.arraycopy(buf, 0, newBuf, 0, curBufLength);
            buf = newBuf;
            curBufLength = newBufLength;
        }

        buf[count] = (byte) b;
        count++;
    }

    public void flush() {
        if (count <= 1) {
            count = 0;
            return;
        }
        final byte[] bytes = new byte[count];
        System.arraycopy(buf, 0, bytes, 0, count);
        String str = new String(bytes);
        log.log(level, str);
        count = 0;
    }

    public void close() {
        flush();
        hasBeenClosed = true;
    }
}

然后为系统输出流创建一个自定义记录器,而不是对其进行注册。

这是记录器用法的完整代码:

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <Properties>
        <Property name="log-pattern">%d{ISO8601} %-5p %m\n</Property>
    </Properties>
    <appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout>
                <pattern>${log-pattern}</pattern>
            </PatternLayout>
        </Console>
    </appenders>
    <Loggers>
        <logger name="testLogger" level="info" additivity="false">
            <AppenderRef ref="Console"/>
        </logger>
        <logger name="systemOut" level="info" additivity="true"/>
    </Loggers>
</configuration>

SystemLogging.java

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;

import java.io.PrintStream;

public class SystemLogging {
    public void enableOutStreamLogging() {
        System.setOut(createPrintStream("systemOut", Level.INFO));
    }

    private PrintStream createPrintStream(String name, Level level) {
        return new PrintStream(new LoggingOutputStream(LogManager.getLogger(name), level), true);
    }
}

LogApp.java

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class LogApp {
    public static void main(String[] args) {
        new SystemLogging().enableOutStreamLogging();

        Logger log = LogManager.getLogger("testLogger");
        log.info("Logger output test!");
        System.out.println("System out test!");
    }
}

最终输出

2019-01-08T19:30:43,456 INFO  Logger output test!
19:30:43.457 [main] INFO  systemOut - System out test!

现在,根据需要使用新的记录器配置自定义系统。

加上;如果您不想覆盖System.out而只想保存它:TeeOutputStream库中有commons-io。您只需将原始System.outSystem.out的组合替换为原始LoggingOutputStreamSystem.out即可同时写入两个流。这不会更改原始输出,但允许您使用日志记录附加程序保存variable "AutoStopSchedule" { default = "5" } locals{ schedule_expression= "cron(${var.AutoStopSchedule} * * * ? *)" } output "schedule_expression" { value = "${local.schedule_expression}" }