如何创建自定义 logback 转换规则不是 xml?

问题描述

我正在尝试创建一个控制台 appender,它使用自定义模式和自定义转换规则通过 DataConverter 类运行 logback 日志。我可以在日志中看到新的转换规则被创建,但在那之后日志被删除。我假设以下代码中的某些设置不正确,但我不确定我哪里出错了。

ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<>()
appender.setName("consoleAppender")

RuleStore rs = new SimpleRuleStore(context)

Interpreter interpreter = new Interpreter(context,rs,new ElementPath())
InterpretationContext interpretationContext = interpreter.getInterpretationContext()
interpretationContext.setContext(context)

AttributesImpl attributes = new AttributesImpl()
attributes.addAttribute("","","conversionWord","msgMask")
attributes.addAttribute("","converterClass","com.x.logging.library.DataConverter")

ConversionRuleAction conversionRule = new ConversionRuleAction()
conversionRule.setContext(context)
conversionRule.begin(interpretationContext,conversionWord,attributes)

PatternLayoutEncoder encoder = new PatternLayoutEncoder()
encoder.setContext(context)
encoder.setPattern('-%clr(%d{-yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(-%5p) %clr(-){magenta} %clr(---){faint} %clr([%t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %msgMask(%msg) %n')
encoder.start()

appender.setEncoder(encoder)
appender.setContext(context)
appender.start()

//the appender is later added to the logger

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)