玩框架logback自定义布局

2024-01-13

我正在尝试使用自定义布局类来进行 Play Framework 2.0 logback 日志记录。

首先,我在包 utils 中定义了一个自定义布局类:

package utils;

public class MonitorLayoutForLogback extends LayoutBase<ILoggingEvent> {
...
}

在我的 conf/logging.xml 文件中,我输入:

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <layout class="utils.MonitorLayoutForLogback">
                             <param name="programName" value="uowVisualizer" />
                             <param name="serviceGroup" value="shared" />
                             <param name="serviceIdentifier" value="uowVisualizer" />
            </layout>
        </encoder>
    </appender>

但是当我在比赛中跑步时,例如,

play run

I see:

14:20:18,387 |-ERROR in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Could not create component [layout] of type [utils.MonitorLayoutForLogback] java.lang.ClassNotFoundException: utils.M
onitorLayoutForLogback
    at java.lang.ClassNotFoundException: utils.MonitorLayoutForLogback
    at      at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at      at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at      at java.security.AccessController.doPrivileged(Native Method)
    at      at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at      at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at      at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at      at sbt.PlayCommands$$anonfun$53$$anonfun$55$$anon$2.loadClass(PlayCommands.scala:535)
    at      at ch.qos.logback.core.util.Loader.loadClass(Loader.java:124)
    at      at ch.qos.logback.core.joran.action.NestedComplexPropertyIA.begin(NestedComplexPropertyIA.java:100)
    at      at ch.qos.logback.core.joran.spi.Interpreter.callBeginAction(Interpreter.java:276)
    at      at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:148)
    at      at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:130)
    at      at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:50)
    at      at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:157)
    at      at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:143)
    at      at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:106)
    at      at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:56)
    at      at play.api.Logger$$anonfun$configure$8.apply(Logger.scala:248)
    at      at play.api.Logger$$anonfun$configure$8.apply(Logger.scala:247)
    at      at scala.Option.map(Option.scala:145)
    at      at play.api.Logger$.configure(Logger.scala:247)
    at      at play.api.Application$class.$init$(Application.scala:266)

所以,play找不到我创建的布局类。如何将布局类放在类路径上?

请注意,我还尝试通过以下方式暂存该项目:

play clean compile stage

然后通过启动该项目

target/start

从打包版本启动项目,我没有看到上面的丢失类错误。但是,我也从未看到任何输出,甚至也没有看到构造的类。我向此类的每个构造函数添加了 System.out.println 语句,如下所示,以验证是否正在构造该类:

    public MonitorLayoutForLogback() {
        System.out.println("MonitorLayoutForLogback Constructor without arguments");
    }

    public MonitorLayoutForLogback(String program) {
        System.out.println("MonitorLayoutForLogback Constructor with program "+program);
        _program = program;
    }

    public MonitorLayoutForLogback(String program, String sGroup, String sid) {
        System.out.println("MonitorLayoutForLogback Constructor with program "+program+" sGroup "+sGroup+" sid "+sid);
        _program = program;
        MonitoringInfo.setServiceGroup(sGroup);
        MonitoringInfo.setServiceIdentifier(sid);
    }

我是 logback 配置的新手,所以我确信我错过了一些明显的东西。谢谢您的帮助。


您看到的问题源于 logback 如何使用类加载器来配置为过滤器、布局、编码器等的类。

问题是,对于所有依赖项(包括 logback),类都加载在DependencyClassloader这是稳定的,而项目代码加载在ReloadableClassloader它是稳定类加载器的子类,每当项目源代码更改时就会被丢弃。

由于 logback 不允许传递自定义类加载器,也不允许查找上下文类加载器,因此它尝试解析稳定类加载器中的项目类,但无法找到项目类。

有一个拒绝拉取请求 https://github.com/qos-ch/logback/pull/255改变 logback 中的行为 有证据表明没有计划 https://github.com/qos-ch/logback/pull/255#issuecomment-375413563改变这种行为

有两种解决方法:

  • 如果您正在使用子项目,请将您的班级放入子项目中
  • 如果您不使用子项目,请将您的类打包到jar文件并把它jar文件到lib/目录 https://www.playframework.com/documentation/2.6.x/Anatomy#The-lib/-directory你的项目的
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

玩框架logback自定义布局 的相关文章

随机推荐