玩框架logback自定义布局

Chr*_*nes 13 logback playframework

我正在尝试使用自定义布局类来进行play framework 2.0 logback日志记录.

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

package utils;

public class MonitorLayoutForLogback extends LayoutBase<ILoggingEvent> {
...
}
Run Code Online (Sandbox Code Playgroud)

在我的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>
Run Code Online (Sandbox Code Playgroud)

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

play run
Run Code Online (Sandbox Code Playgroud)

我知道了:

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)
Run Code Online (Sandbox Code Playgroud)

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

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

play clean compile stage
Run Code Online (Sandbox Code Playgroud)

然后通过开始项目

target/start
Run Code Online (Sandbox Code Playgroud)

从打包版本启动项目,我没有看到上面缺少的类错误.但是,我也从未看到任何输出,也没有看到构造的类.我将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);
    }
Run Code Online (Sandbox Code Playgroud)

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

小智 -2

对我来说,我在启动我的应用程序时看到了这个错误,如下所示:

./activator -Dhttp.port=9000 -Dconfig.resource=local.conf -jvm-debug 9999 run
Run Code Online (Sandbox Code Playgroud)

但我通过使用 start 而不是 run 克服了这个问题

./activator -Dhttp.port=9000 -Dconfig.resource=local.conf -jvm-debug 9999 start
Run Code Online (Sandbox Code Playgroud)

然而,这又产生了另一个问题;说找不到application.conf。我这样指定文件并不重要:

-Dconfig.resource=local.conf
Run Code Online (Sandbox Code Playgroud)

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