Playframework 播放框架回写自定义布局

Playframework 播放框架回写自定义布局,playframework,logback,Playframework,Logback,我正在尝试为play framework 2.0 logback日志使用自定义布局类 首先,我在包utils中定义了一个自定义布局类: package utils; public class MonitorLayoutForLogback extends LayoutBase<ILoggingEvent> { ... } 我明白了: 14:20:18,387 |-ERROR in ch.qos.logback.core.joran.action.NestedComplexProp

我正在尝试为play framework 2.0 logback日志使用自定义布局类

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

package utils;

public class MonitorLayoutForLogback extends LayoutBase<ILoggingEvent> {
...
}
我明白了:

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配置是个新手,所以我肯定我遗漏了一些明显的东西。谢谢您的帮助。

对于我来说,我在启动我的应用程序时看到了以下错误:

./activator -Dhttp.port=9000 -Dconfig.resource=local.conf -jvm-debug 9999 run
但我通过使用start而不是run来克服这个问题

./activator -Dhttp.port=9000 -Dconfig.resource=local.conf -jvm-debug 9999 start
然而,这又产生了另一个问题;表示找不到application.conf。我这样指定文件并不重要:

-Dconfig.resource=local.conf

将local.conf重命名为application.conf后,将找到并使用用于日志记录的自定义布局类。

您看到的问题源于logback如何将类加载器用于配置为筛选器、布局、编码器等的类

问题是,对于所有依赖项,包括logback,类加载在稳定的
DependencyClassloader
中,而项目代码加载在稳定类加载程序的子类
ReloadableClassloader
中,并在项目源代码更改时丢弃

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

在logback中,有一种方法可以改变这种行为 有必要改变这种行为

有两种解决办法:

  • 如果您使用子项目,请将您的类放入子项目中
  • 如果不使用子项目,请将类打包到
    jar
    文件中,并将该
    jar
    文件放入项目的

使自定义记录器类正常工作的原因是什么?从你的回答来看,所采取的步骤并不清楚。
./activator -Dhttp.port=9000 -Dconfig.resource=local.conf -jvm-debug 9999 start
-Dconfig.resource=local.conf