gpt4 book ai didi

java - logback中动态添加Appender

转载 作者:太空宇宙 更新时间:2023-11-04 11:25:48 27 4
gpt4 key购买 nike

我有一个来自 slf4j 的记录器实例。该记录器已经记录到控制台。我想向其中添加一个附加附加程序,它将为每个线程单独记录到一个文件中。所以我想使用 SiftingAppender

我创建了以下 utils 类来将附加程序添加到记录器实例。

package com.worksap.company.hue.com.bizcore.outputengine.devtools;

import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.sift.MDCBasedDiscriminator;
import ch.qos.logback.classic.sift.SiftingAppender;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.rolling.RollingFileAppender;
import ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP;
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy;
import ch.qos.logback.core.sift.AppenderFactory;

public class Utils {

public static Logger putNewAppender(String file, org.slf4j.Logger log) {

LoggerContext lc = (LoggerContext)LoggerFactory.getILoggerFactory();
Logger logger = lc.getLogger(log.getName());
SiftingAppender sa = new SiftingAppender();
sa.setName("SIFT");
sa.setContext(lc);

MDCBasedDiscriminator discriminator = new MDCBasedDiscriminator();
discriminator.setKey("logFileName");
discriminator.setDefaultValue("head0");
discriminator.start();

sa.setDiscriminator(discriminator);

sa.setAppenderFactory(new AppenderFactory<ILoggingEvent>() {

@Override
public Appender<ILoggingEvent> buildAppender(Context context, String discriminatingValue)
throws JoranException {
RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>();
appender.setName("ROLLINGFILE-" + discriminatingValue);
appender.setContext(context);
appender.setFile(discriminatingValue + ".log");

TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<>();
policy.setContext(context);
policy.setMaxHistory(5);
policy.setFileNamePattern(discriminatingValue + "-%d{yyyy-MM-dd-HH-mm}-%i.log.gz");
policy.setParent(appender);
policy.start();

SizeAndTimeBasedFNATP<ILoggingEvent> innerpolicy = new SizeAndTimeBasedFNATP<>();
innerpolicy.setContext(context);
innerpolicy.setMaxFileSize("512KB");
innerpolicy.setTimeBasedRollingPolicy(policy);
innerpolicy.start();

policy.setTimeBasedFileNamingAndTriggeringPolicy(innerpolicy);
policy.start();

appender.setRollingPolicy(policy);

PatternLayoutEncoder pl = new PatternLayoutEncoder();
pl.setContext(context);
pl.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
pl.start();
appender.setEncoder(pl);

appender.start();
return appender;
}
});

sa.start();
logger.addAppender(sa);
logger.setAdditive(false);

return logger;

}
}

我正在尝试在这里测试日志:

@Slf4j
public class IfxOutputJobToolTest {

@Test
public void outputTest() throws Exception {

Logger logger2 = Utils.putNewAppender("C:\\Users\\works\\Downloads\\somename", log);
MDC.put("logFileName", "somename");
logger2.info("something is getting logged");
logger2.error("somerghing");
logger2.warn("somrgklsajg");
logger2.debug("ser");
MDC.remove("logFileName");

LoggerContext context = (LoggerContext)LoggerFactory.getILoggerFactory();
for (ch.qos.logback.classic.Logger logger3 : context.getLoggerList()) {
for (Iterator<Appender<ILoggingEvent>> index = logger3.iteratorForAppenders(); index.hasNext();) {
Appender<ILoggingEvent> appender = index.next();
System.out.println(appender);
}
}
}
}

控制台输出如下:

ch.qos.logback.classic.sift.SiftingAppender[SIFT]
ch.qos.logback.core.ConsoleAppender[CONSOLE]
ch.qos.logback.core.ConsoleAppender[CONSOLE]
ch.qos.logback.core.ConsoleAppender[CONSOLE_SPRING]

但是,没有任何记录器信息实际上打印在控制台或文件中。如果我不添加 siftingAppender,它会在控制台上正确打印。如何以编程方式添加 SiftingAppender

我无权更改logback.xml

启用 logback 登录后,我得到以下信息:

16:40:08,943 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use gz compression
16:40:08,946 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use the pattern somename-%d{yyyy-MM-dd-HH-mm}-%i.log for the active file
16:40:08,956 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd-HH-mm' from file name pattern 'somename-%d{yyyy-MM-dd-HH-mm}-%i.log.gz'.
16:40:08,956 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over every minute.
16:40:08,957 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Tue Jun 06 16:35:47 JST 2017
16:40:08,962 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@77a4bac - The date pattern is 'yyyy-MM-dd-HH-mm' from file name pattern 'somename-%d{yyyy-MM-dd-HH-mm}-%i.log.gz'.
16:40:08,962 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@77a4bac - Roll-over every minute.
16:40:08,962 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@77a4bac - Setting initial period to Tue Jun 06 16:35:47 JST 2017
16:40:08,964 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use gz compression
16:40:08,964 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use the pattern somename-%d{yyyy-MM-dd-HH-mm}-%i.log for the active file
16:40:08,965 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@77a4bac - The date pattern is 'yyyy-MM-dd-HH-mm' from file name pattern 'somename-%d{yyyy-MM-dd-HH-mm}-%i.log.gz'.
16:40:08,966 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@77a4bac - Roll-over every minute.
16:40:08,966 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@77a4bac - Setting initial period to Tue Jun 06 16:35:47 JST 2017
16:40:08,969 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ROLLINGFILE-somename] - Active log file name: somename.log
16:40:08,969 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[ROLLINGFILE-somename] - File property is set to [somename.log]
16:40:08,975 |-INFO in c.q.l.co.rolling.helper.RenameUtil - Renaming file [somename.log] to [somename.log91512733365898.tmp]
16:40:08,979 |-INFO in ch.qos.logback.core.rolling.helper.SizeAndTimeBasedArchiveRemover@96be1ae - first clean up after appender initialization
16:40:08,979 |-INFO in ch.qos.logback.core.rolling.helper.SizeAndTimeBasedArchiveRemover@96be1ae - periodsElapsed = 336
16:40:08,979 |-INFO in ch.qos.logback.core.rolling.helper.Compressor - GZ compressing [somename.log91512733365898.tmp] as [somename-2017-06-06-16-35-0.log.gz]

编辑

删除此行logger.setAdditive(false);后,日志将打印在控制台上。但是,我看不到文件被创建。

最佳答案

您在附加器工厂中构建附加器的方式似乎存在一些问题。不确定,确切的问题是什么,但我尝试了下面的代码并且它有效。`

sa.setAppenderFactory(new AppenderFactory() {

                 public Appender<ILoggingEvent> buildAppender(Context context, String discriminatingValue)
throws JoranException {
PatternLayoutEncoder ple = new PatternLayoutEncoder();

ple.setPattern("%date [%thread] [%file:%line] %msg%n");
ple.setContext(context);
ple.start();


RollingFileAppender<ILoggingEvent> logFileAppender = new RollingFileAppender();
logFileAppender.setContext(context);
logFileAppender.setName("File-"+discriminatingValue);
logFileAppender.setEncoder(ple);
logFileAppender.setFile(filename+"-"+discriminatingValue+".txt");

SizeAndTimeBasedRollingPolicy<ILoggingEvent> logFilePolicy = new SizeAndTimeBasedRollingPolicy();
logFilePolicy.setContext(context);
logFilePolicy.setParent(logFileAppender);
logFilePolicy.setFileNamePattern(filename+"-"+discriminatingValue+"-%d{yyyy-MM-dd}.%i.txt");
logFilePolicy.setMaxHistory(5);
logFilePolicy.setMaxFileSize(FileSize.valueOf("512kb"));
logFilePolicy.setTotalSizeCap(FileSize.valueOf("1gb"));
logFilePolicy.start();

logFileAppender.setRollingPolicy(logFilePolicy);
logFileAppender.start();

logFileAppender.start();
return logFileAppender;
}
});

`

关于java - logback中动态添加Appender,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44383850/

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