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

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

问题描述

我在Jetty 6上运行的Linux上有一个Solr服务器,我正在尝试为java日志记录设置自定义格式化程序,但我似乎无法识别我的自定义类。我是Java的新手,所以引用它可能是我如何导出我的类或类似的问题。请注意,这几乎是可以找到的问题这里,但是那里的答案没有用,因为我有一个公共的无参数构造函数。

I have a Solr server on Linux running under Jetty 6 and am trying to set up a custom formatter for java logging however I can't seem to get it to recognize my custom class. I am new to Java so it is quote possible it is an issue with how I am exporting my class or something like that. Note this is almost the same question as can be found here, however the answer there does not help since I do have a public no-parameter constructor.

我的格式化程序如下所示(如描述< a href =http://www.javalobby.org/java/forums/t18515.html\"rel =nofollow noreferrer>这里):

My formatter looks like the following (as described here):

package myapp.solr;

import java.text.MessageFormat;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;

public class LogFormatter extends Formatter {

  private static final MessageFormat fmt = new MessageFormat("{0,date,yyyy-MM-dd HH:mm:ss} {1} [{2}] {3}\n");

  public LogFormatter() {
    super();
  }

  @Override public String format(LogRecord record) {
    Object[] args = new Object[5];
    args[0] = new Date(record.getMillis());
    args[1] = record.getLevel();
    args[2] = record.getLoggerName() == null ? "root" : record.getLoggerName();
    args[3] = record.getMessage();
    return fmt.format(args);
  }

}

在我的logging.properties文件中我然后有以下(以及配置文件路径/模式和旋转限制和计数的属性):

In my logging.properties file I then have the below (as well as properties to configure the file path/pattern and rotation limit and count) :

handlers = java.util.logging.FileHandler
java.util.logging.FileHandler.formatter = myapp.solr.LogFormatter

然后我将我的类导出到myapp.jar并将它放在jetty.home的lib / ext文件夹中(我也尝试将它直接放在lib下,我尝试使用-Djetty.class指定它的路径。路径参数)。但是,当我运行我的solr应用程序时,它仍然使用XmlFormatter。我能够成功地将其更改为使用SimpleFormatter,而不是我自己的自定义格式化程序。

I then export my class into myapp.jar and put it in the lib/ext folder in jetty.home (I also tried placing it directly under lib and I tried specifying the path to it with the -Djetty.class.path parameter). However when I run my solr app it still uses the XmlFormatter instead. I am able to successfully change it to use the SimpleFormatter, just not my own custom formatter.

我还创建了一个测试类,用于导入我的LogFormatter,创建实例变量和调用format方法并将结果打印到控制台,并且在Eclipse中没有任何问题。

I also created a test class that imports my LogFormatter, creates an instance variable and calls the format method and prints the result to the console and that worked without any issues from within Eclipse.

如果有帮助,我用来启动Solr /的命令Jetty是:

If it helps, the command I am using to start up Solr/Jetty is:

nohup java -DSTOP.PORT=8079 -DSTOP.KEY=secret -Dsolr.solr.home=../solr_home/local -Djava.util.config.file=../solr_home/local/logging.properties -jar start.jar > /var/log/solr/stdout.log 2&>1 &

那么我做错了什么,为什么不使用我的自定义格式化程序?

So what am I doing wrong, why won't it use my custom formatter?

推荐答案

感谢一些有用的建议这里。问题是在从lib或lib / ext文件夹加载自定义类之前设置了java日志记录,因此我需要将它添加到start.jar中。

Got it working thanks to some useful suggestions here. The problem was that the java logging is set up before the custom class is loaded from the lib or lib/ext folders so I needed to add it into the start.jar.

我是如何做到这一点的,我创建了一个自己的新包,我调用了org.mortbay.start并添加了我的自定义LogFormatter类。 Eclipse自动从bin / org / mortbay / start文件夹中构建LogFormatter.class。然后我打开了start.jar归档并将我的自定义类添加到其中,所以我有start.jar / org / mortbay / start / LogFormatter.class。一旦那样,我就可以使用以下方式设置格式化程序:

How I did that was I created a new package of my own that I called org.mortbay.start and added my custom LogFormatter class to that. Eclipse automatically built the LogFormatter.class from that in the bin/org/mortbay/start folder. I then opened the start.jar archive up and added my custom class into it so I had start.jar/org/mortbay/start/LogFormatter.class. Once that was there I was then able to set my formatter using:

handlers = java.util.logging.FileHandler
java.util.logging.FileHandler.formatter = org.mortbay.start.LogFormatter

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

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