gpt4 book ai didi

java - 以编程方式创建 logback 记录器

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:33:02 26 4
gpt4 key购买 nike

我有一个关于 logback 项目的问题。我的要求是我必须动态创建日志属性。让我通过一个例子来解释这一点。

我的项目创建与外部系统的套接字通信,它可能有多个套接字。对于每个套接字,我想要不同的日志文件,其中包含读取和发送的消息。为此,我以编程方式为套接字创建记录器。问题是当我想基于 logback.xml 重新配置记录器(通过添加 scan="true"或重新初始化 logback)时,我创建的记录器变得不可用。我该如何解决这个问题,或者您能建议我另一种解决方案吗?

这是我的配置文件(logback.xml)

<?xml version="1.0" ?>
<configuration>
<property name="HOME_PATH" value="/data/logs/myapp/" scope="CONTEXT" />
<property name="MYAPP_LOG_FILE" value="myapp.log" />
<property name="MYAPP_ROLLING_TEMPLATE" value="%d{yy-MM-dd}" scope="CONTEXT" />
<property name="MYAPP_OLD_LOG_FILE" value="${MYAPP_LOG_FILE}.%d{yy-MM-dd}" />
<property name="DEFAULT_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%file:%line] [%level] %msg%n" scope="CONTEXT" />

<appender name="myAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${HOME_PATH}${MYAPP_LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${HOME_PATH}${MYAPP_LOG_FILE}.${MYAPP_ROLLING_TEMPLATE}</fileNamePattern>
</rollingPolicy>

<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>${DEFAULT_PATTERN}</pattern>
</encoder>
</appender>

<logger name="com.myapp" level="DEBUG" additivity="false">
<appender-ref ref="myAppender" />
</logger>

<root level="OFF">
</root>
</configuration>

在这里您可以看到我如何以编程方式创建记录器(同样,我这样做只是为了套接字日志)。

public static Logger createLogger(String name) {
ch.qos.logback.classic.Logger templateLogger = (ch.qos.logback.classic.Logger) LogUtil.getLogger("com.myapp");
LoggerContext context = templateLogger.getLoggerContext();

String logDir = context.getProperty("HOME_PATH");

PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setPattern(context.getProperty("DEFAULT_PATTERN"));
encoder.setContext(context);

DefaultTimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent> timeBasedTriggeringPolicy = new DefaultTimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent>();
timeBasedTriggeringPolicy.setContext(context);

TimeBasedRollingPolicy<ILoggingEvent> timeBasedRollingPolicy = new TimeBasedRollingPolicy<ILoggingEvent>();
timeBasedRollingPolicy.setContext(context);
timeBasedRollingPolicy.setFileNamePattern(logDir + name + ".log." + context.getProperty("MYAPP_ROLLING_TEMPLATE"));
timeBasedRollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(timeBasedTriggeringPolicy);
timeBasedTriggeringPolicy.setTimeBasedRollingPolicy(timeBasedRollingPolicy);

RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<ILoggingEvent>();
rollingFileAppender.setAppend(true);
rollingFileAppender.setContext(context);
rollingFileAppender.setEncoder(encoder);
rollingFileAppender.setFile(logDir + name + ".log");
rollingFileAppender.setName(name + "Appender");
rollingFileAppender.setPrudent(false);
rollingFileAppender.setRollingPolicy(timeBasedRollingPolicy);
rollingFileAppender.setTriggeringPolicy(timeBasedTriggeringPolicy);

timeBasedRollingPolicy.setParent(rollingFileAppender);

encoder.start();
timeBasedRollingPolicy.start();

rollingFileAppender.stop();
rollingFileAppender.start();

ch.qos.logback.classic.Logger logbackLogger = (ch.qos.logback.classic.Logger) LogUtil.getLogger(name);
logbackLogger.setLevel(templateLogger.getLevel());
logbackLogger.setAdditive(false);
logbackLogger.addAppender(rollingFileAppender);

return logbackLogger;
}

这就是我重新初始化 logback 的方式

private static void initializeLogback() {
File logbackFile = new File(logFilePath);
System.setProperty("logback.configurationFile", logbackFile.getAbsolutePath());
StaticLoggerBinder loggerBinder = StaticLoggerBinder.getSingleton();
LoggerContext loggerContext = (LoggerContext) loggerBinder.getLoggerFactory();

loggerContext.reset();
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(loggerContext);
try {
configurator.doConfigure(logbackFile);
} catch( JoranException e ) {
throw new ColumbusRuntimeException(e.getMessage(), e);
}
}

最佳答案

看起来您需要 SiftingAppender您的鉴别器将是套接字 ID 本身或任何组合变体。我不知道您会遇到什么线程问题(当读取 MDC 时等),但这应该是一个很好的起点,并且看起来与您的情况相似。

关于java - 以编程方式创建 logback 记录器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9060545/

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