SolrJetty logging - how to get custom log formatte

2019-05-29 14:47发布

问题:

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.

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

}

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

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.

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.

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?

回答1:

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.

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