logback - 如何创建自定义 logback 转换规则(不是 xml)?
问题描述
我正在尝试创建一个控制台附加程序,它使用自定义模式和自定义转换规则通过 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
解决方案
推荐阅读
- swift - 取消激活将它们链接在一起的 2 个对象之间的约束
- dart - Dart Future 和明确的解决,拒绝
- html - 带有 IMAGE 和 PDF 的 HTML 电子邮件
- php - 如何在特定位置插入字符串中的字符?
- python - 初始化无多维数组
- css - 使用 font-face 时出现 ERR_ABORTED 500 (Internal Server Error) 的原因是什么?
- javascript - 尝试从嵌套的 redux 对象中删除所有项目并添加传入项目
- fable-f# - 在 Fable-Elmish 中处理自定义事件
- c++ - QPixmap 未显示在 Qt 的对话框中
- javascript - 导入节点模块与自定义组件 ES6、React