播放框架logback自定义布局 [英] play framework logback custom layout

查看:137
本文介绍了播放框架logback自定义布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将自定义布局类用于Play Framework 2.0 logback日志记录.

I am trying to use a custom layout class for play framework 2.0 logback logging.

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

First, I defined a custom layout class in package utils:

package utils;

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

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

In my conf/logging.xml file, I put:

<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>

但是当我在游戏中跑步时,例如

but when I run within play, e.g.,

play run

我知道了

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)

因此,游戏无法找到我创建的布局类.如何将布局类放在类路径上?

So, play can't find the layout class I created. How do I put the layout class on the class path?

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

Note that I also tried staging the project via,

play clean compile stage

然后通过

target/start

从打包版本开始项目,我看不到上面缺少的类错误.但是,我也永远都看不到任何输出,甚至也看不到构造的类.我向该类的每个构造函数添加了System.out.println语句,如下所示,以验证是否正在构造该类:

Starting the project from the packaged version, I don't see the above missing class error. However, I also never see any output, nor do I even see the class constructed. I added System.out.println statements to each constructor for this class as follows, to verify whether or not the class was being constructed:

    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);
    }

我是重新登录配置的新手,所以我确定我缺少明显的东西.感谢您的帮助.

I'm a newbie to logback configuration, so I'm sure I'm missing something obvious. Thanks for the help.

推荐答案

您看到的问题源于logback如何将类加载器用于配置为过滤器,布局,编码器等的类.

The issue you are seeing stems from how logback uses the class loader for classes configured as filter, layout, encoder etc.

问题在于,对于所有依赖项,包括重新登录,这些类均装入稳定的DependencyClassloader中,而项目代码则装入稳定类装入器的子级ReloadableClassloader中,并被丢弃每当项目源代码更改时.

The issue is that for all dependencies, including logback, the classes are loaded in a DependencyClassloader which is stable, while the project code is loaded in a ReloadableClassloader which is a child of the stable class loader and is discarded whenever project source code changes.

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

Since logback doesn't allow to pass a custom classloader, nor does it lookup the context classloader, it tries to resolve project classes in the stable classloader and fails to find project classes.

有一个拒绝的拉取请求可以更改登录中的行为 有证据表明没有计划来改变这种行为

There is a declined pull request to change that behaviour in logback There is evidence there are no plans to change that behaviour

有两种解决方法:

  • 如果您正在使用子项目,请将您的课程放到子项目中
  • 如果您不使用子项目,请将您的类打包到jar文件中,然后将该jar文件放入
  • If you are using sub-projects put your class into a sub-project
  • If you are not using sub-projects package your class into a jar file and put that jar file into the lib/ directory of your project

这篇关于播放框架logback自定义布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆