Jetty6 slf4j 日志记录 - 如何让自定义日志格式化程序工作?

发布于 2024-09-28 15:30:04 字数 1840 浏览 0 评论 0原文

我正在使用 jetty6 与 SLF4J 和 java 日志记录,并一直在尝试添加自定义日志格式化程序,但无论我尝试什么,我似乎都无法让它工作。

我有一个格式化程序,如下所示:

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

我想使用它作为我的 jetty 日志记录的默认值,因此我创建了一个 ${jetty.home}/resources/logging.properties 文件:

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 类。我把这个 jar 放入 ${jetty.home}/lib/ext 中。

我启动 jetty:

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

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

如果我更改logging.properties文件来设置如下所示的格式化程序:

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

...然后它就可以工作-日志文件是使用普通的SimpleFormatter写出的。所以我确信我的属性没问题,并且我的 slfj 罐子等都正确。只是 Jetty 不喜欢我的 DeadSimpleFormatter。

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

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

...但没有乐趣。

我将一个 main 方法放入 DeadSimpleFormatter 中,并检查我是否可以运行该 jar:

java -jar lib/ext/logging.jar 

...这有效,所以我很确定我的 jar 没问题。

有谁知道这里发生了什么事吗?我已经尝试了所有我能想到的组合。

谢谢, 阿拉斯泰尔

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

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

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

I start jetty:

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

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.

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

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

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

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

...but no joy.

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.

Thanks,
Alastair

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲鸠 2024-10-05 15:30:04

为了使自定义格式化程序能够通过配置工作,它应该是公共的,并且具有公共的无参数构造函数。由于 java.util.logging.LogManager 尝试通过反射机制初始化此格式化程序,如果失败,则使用默认格式化程序。

For custom formatter to work from configuration it should be public and have public no-parameter cunstructor. Since java.util.logging.LogManager attempts to initialize this formatter through reflection mechanizm and if it fails it uses default one.

甜扑 2024-10-05 15:30:04

阿拉斯泰尔,不确定你是否曾经让它工作过,但我相信我遇到了与你相同的问题,并且我让它工作了。您可以在此处查看我的问题和答案,但其要点是:

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

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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文