Jetty6 slf4j日志记录-如何使自定义日志格式化程序正常工作? [英] Jetty6 slf4j logging - how to get custom log Formatter to work?

查看:112
本文介绍了Jetty6 slf4j日志记录-如何使自定义日志格式化程序正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有SLF4J和Java日志记录的jetty6,并且一直在尝试添加自定义日志格式化程序,但是无论我如何尝试,我似乎都无法使其正常工作.

I'm using jetty6 with SLF4J and java logging and have been trying to add a custom log Formatter, but no matter what I try I can't seem to get it to work.

我有一个格式化程序,像这样:

I have a Formatter, like this:

package mycode.logging;
public class DeadSimpleFormatter extends SimpleFormatter
{
  // Nothing here at all - just an empty subclass of SimpleFormatter.
}

我想将此作为我的码头日志记录的默认值,所以我创建了一个$ {jetty.home}/resources/logging.properties文件:

I want to use this as the default for my jetty logging, so I've created a ${jetty.home}/resources/logging.properties file:

handlers=java.util.logging.FileHandler
.level=INFO
java.util.logging.FileHandler.pattern=logs/test_%u.%g.log
java.util.logging.FileHandler.limit=90000
java.util.logging.FileHandler.count=20
java.util.logging.FileHandler.formatter=mycode.logging.DeadSimpleFormatter

mycode.level=INFO

我创建一个jar文件logging.jar,其中包含DeadSimpleFormatter类.我把这个罐子放到了$ {jetty.home}/lib/ext.

I create a jar file logging.jar, containing the DeadSimpleFormatter class. I put this jar into ${jetty.home}/lib/ext.

我开始码头:

java -Djava.util.logging.config.file=resources/logging.properties 
    -jar start.jar etc/jetty-logging.xml etc/jetty.xml

我可以看到正在创建输出文件.它遵循我的属性文件中定义的限制和计数规则.但是它不使用我的格式化程序-它恢复为默认的XmlFormatter.我没有看到stdout或stderr出现任何错误.

I can see the output file being created. It follows the rules for limit and count as defined in my properties file. But it doesn't use my formatter - it reverts to the default XmlFormatter. I don't see any errors out of stdout or stderr.

如果我更改logging.properties文件以设置格式器,如下所示:

If I change the logging.properties file to set the formatter like this:

java.util.logging.FileHandlerformatter=java.util.logging.SimpleFormatter

...然后它起作用了-使用普通的SimpleFormatter写入日志文件.因此,我确信自己的设备还可以,并且我的slfj罐子等都正确.只是Jetty不喜欢我的DeadSimpleFormatter.

...then it works - the log file is written out using the normal SimpleFormatter. So I'm confident that my properties are ok and I have my slfj jars etc. all correct. It's just that Jetty doesn't like my DeadSimpleFormatter.

因为什么都没有-从字面上看! -在DeadSimpleFormatter中,我认为这可能是类加载问题.我尝试像这样显式添加jar文件:

Since there's nothing - literally! - in DeadSimpleFormatter, I figure this may be a class loading issue. I tried explicitly adding the jar file like this:

java -Djetty.class.path=/mypathtojettyhome/lib/ext/logging.jar
    -Djava.util.logging.config.file=resources/logging.properties -jar start.jar 
    etc/jetty-logging.xml etc/jetty.xml

...但是不高兴.

我在DeadSimpleFormatter中放入了一个主方法,并检查了我是否可以运行jar:

I put a main method into my DeadSimpleFormatter and checked that I could run the jar:

java -jar lib/ext/logging.jar 

...这行得通,所以我很确定我的罐子还可以.

...This works, so I'm pretty sure my jar is ok.

有人知道这里发生了什么吗?我尝试了所有我能想到的组合.

Does anyone have any idea what's going on here? I've tried every combination I can think of.

谢谢, 阿拉斯泰尔

推荐答案

Alastair,不确定您是否曾经使用过此功能,但我认为我遇到了与您相同的问题,并且我也使用了它.您可以在此处看到我的问题和答案. ,但要点是:

Alastair, not sure if you ever got this working but I believe I was having the same issue as you and I got it working. You can see my question and answer here, but the gist of it was:

在将lib或lib/ext文件夹添加到类路径并加载自定义类之前,将加载Java日志记录.我要做的就是使用与包包相同的名称(即start.jar(例如org.mortbay.start)中的主路径)创建我的自定义类,然后将编译后的.class文件添加到该路径中.直接在start.jar中并在我的logging.properties中进行设置,以使用org.mortbay.start.MyCustomFormatter.

The java logging gets loaded before the lib or lib/ext folders are added to the classpath and your custom class is loaded. What I had to do to get it working was create my custom class using the same package name that is the main path in start.jar (ex. org.mortbay.start) then add the compiled .class file from that into that path in the start.jar directly and set in my logging.properties to use org.mortbay.start.MyCustomFormatter.

这篇关于Jetty6 slf4j日志记录-如何使自定义日志格式化程序正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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