gpt4 book ai didi

java - 动态创建 Logback Loggers 和 Appenders

转载 作者:搜寻专家 更新时间:2023-10-30 21:10:39 25 4
gpt4 key购买 nike

我需要为动态加载和重新加载的类动态创建 Logback 记录器和各种位。这些类(class)的数量不定,但总共不到 150 个。性能是一个大问题,这是一个高度线程化和极度并发的环境,因此使用 MDC 的想法不会实现,因为当出现问题时每秒可能有数千次调用。我也不想直接将记录器传递到类中,我希望他们像典型的那样使用 getLogger 将它们从空中拉出来。

每个类都必须将它的所有错误消息记录到它自己的错误文件中,并将它的所有跟踪消息记录到它自己的跟踪文件中。这两个文件都需要每晚滚动。此外,系统中记录的任何内容也必须最终出现在主系统日志中。

我创建了以下代码片段来尝试这个想法。看起来它会起作用,但结果不是我所期望的。下面是可以直接运行的测试程序:

package logbacktesting;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.filter.LevelFilter;
import ch.qos.logback.core.rolling.RollingFileAppender;
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;
import ch.qos.logback.core.spi.FilterReply;
import ch.qos.logback.core.util.StatusPrinter;
import org.slf4j.LoggerFactory;

public class Main {

public static void main(String[] args) {

Logger templateLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("com.myapp");
LoggerContext loggerContext = templateLogger.getLoggerContext();

String[] nameList = new String[] {"test1.class", "test2.class"};

// Set up the pattern
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(loggerContext);
encoder.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
encoder.start();

// Spin over the names to create all the needed objects
for(int i = 0; i < nameList.length; i++) {

String name = nameList[i];

// Set up the roll over policies and the name when it rolls over
TimeBasedRollingPolicy tracePolicy = new TimeBasedRollingPolicy();
tracePolicy.setContext( loggerContext );
tracePolicy.setFileNamePattern( name + "-Trace-%d{yyyy-MM-dd}.log" );
tracePolicy.setMaxHistory( 30 );

TimeBasedRollingPolicy errorPolicy = new TimeBasedRollingPolicy();
errorPolicy.setContext( loggerContext );
errorPolicy.setFileNamePattern( name + "-Error-logFile.%d{yyyy-MM-dd}.log" );
errorPolicy.setMaxHistory( 30 );

// Set up the filters to ensure things get split as expected
LevelFilter traceFilter = new LevelFilter();
traceFilter.setContext( loggerContext );
traceFilter.setLevel( Level.TRACE );
traceFilter.setOnMatch( FilterReply.ACCEPT );
traceFilter.setOnMismatch( FilterReply.DENY );

LevelFilter errorFilter = new LevelFilter();
errorFilter.setContext( loggerContext );
errorFilter.setLevel( Level.ERROR );
errorFilter.setOnMatch( FilterReply.ACCEPT );
errorFilter.setOnMismatch( FilterReply.DENY );

// Set up the trace and error appenders
RollingFileAppender rollingFileAppenderTrace = new RollingFileAppender();
rollingFileAppenderTrace.setContext(loggerContext);
rollingFileAppenderTrace.setName(name + "-Trace");
rollingFileAppenderTrace.setFile(name + "-Trace.log");
rollingFileAppenderTrace.setEncoder(encoder);
rollingFileAppenderTrace.setRollingPolicy( tracePolicy );
rollingFileAppenderTrace.addFilter( traceFilter );
tracePolicy.setParent( rollingFileAppenderTrace );

RollingFileAppender rollingFileAppenderError = new RollingFileAppender();
rollingFileAppenderError.setContext(loggerContext);
rollingFileAppenderError.setName(name + "-Error");
rollingFileAppenderError.setFile(name + "-Error.log");
rollingFileAppenderError.setEncoder(encoder);
rollingFileAppenderError.setRollingPolicy( errorPolicy );
rollingFileAppenderError.addFilter( errorFilter );
errorPolicy.setParent( rollingFileAppenderError );

// Start everything
tracePolicy.start();
errorPolicy.start();
traceFilter.start();
errorFilter.start();
rollingFileAppenderTrace.start();
rollingFileAppenderError.start();

// attach the rolling file appenders to the logger
Logger logger = (ch.qos.logback.classic.Logger) loggerContext.getLogger(name);
logger.addAppender(rollingFileAppenderTrace);
logger.addAppender(rollingFileAppenderError);

}

StatusPrinter.print(loggerContext);

// Test it to see what happens
for(int i = 0; i < nameList.length; i++) {

String name = nameList[i];

Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(name);
logger.error("error message" );
logger.warn("warn message" );
logger.info("info message" );
logger.debug("debug message" );
logger.trace("trace message" );
}

Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("com.myapp");
logger.error( "generic error message ");


}

}

这是 logback.xml 文件:

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logFile.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>

<root level="debug">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>

</configuration>

如果您运行此代码,它将正确地将所有内容记录到主日志文件中。它将按预期创建 2 个跟踪和 2 个错误日志。但是所有跟踪消息都不会出现在跟踪日志文件中,并且两条错误消息都将出现在 class2 错误文件中。它的行为就像所有 4 个 appender 都被添加到同一个记录器并且只有最后一个被添加的 appender 才真正起作用。我很想知道我在这里做什么。谢谢!

最佳答案

everything into the main log file

这是因为所有记录器都将从 ROOT 记录器“继承”附加程序,您可以使用 logger.setAdditive(false) 来禁用此行为。

但是我不知道你为什么没有任何trace log...

也许这两个链接会给你一些启发

Logback - set log file name programmatically

how-to-create-logback-loggers-dynamically-pragmatically

关于java - 动态创建 Logback Loggers 和 Appenders,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16845067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com