I tried to use Jersey-Spring4 with logback and was not able to redirect access logs via the LoggingFeature
to my logback-logger.
The logback-logger was configured like <logger name="org.glassfish.jersey" level="INFO"/>
and I assumed this should do the trick.
After I learned that Jersey is using JUL, I was sad for a few minutes and carried on with using org.slf4j:jul-to-slf4j
.
To make it work I had to
private void registerLogback(final ServletContext servletContext) {
servletContext.addListener(new LogbackConfigListener());
//this is only necessary if you externalise logback.xml from resources
try { LogbackConfigurer.initLogging(servletContext.getRealPath("/WEB-INF/logback.xml")); }
catch (FileNotFoundException | JoranException e) { e.printStackTrace(); }
LogManager.getLogManager().reset();
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
}
in my WebApplicationInitializer.onStartup(ServletContext servletContext)
.
My ResourceConfig
looked like
register(new LoggingFeature(createLogger(), Level.ALL, LoggingFeature.DEFAULT_VERBOSITY, 0));
private Logger createLogger() {
final ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(Level.ALL);
final Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
logger.setLevel(Level.ALL);
logger.addHandler(consoleHandler);
return logger;
}
What logged access to the console. Removing the ConsoleHandler
sadly did not end up in logs being logged to the logback-logger, the just got swallowed. But the red log statements on the console gave me the hint that this could be STDERR
, albeit their were INFO
.
Finally LoggingFeature not logging correctly based on configuration on constructor gave me the missing piece.
My final and working ResourceConfig
ended up as
register(new LoggingFeature(Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME), Level.INFO, LoggingFeature.DEFAULT_VERBOSITY, 0));
Where LoggingFeature.DEFAULT_LOGGER_NAME
and Level.INFO
seem to be the crucial part.
In the end I don't know exactly went wrong, but this is the result of my guessing and it works. If this helps anybody I am happy.