Jetty6 slf4j logging - how to get custom log Forma

2019-08-07 05:19发布

问题:

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

回答1:

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.



回答2:

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.