Redirecting Guice logs to SLF4J

1,007 views
Skip to first unread message

Gian Marco Gherardi

unread,
Feb 7, 2011, 12:38:22 PM2/7/11
to google...@googlegroups.com
Hi, i'm evaluating Guice for use in one of my projects.
Looking at the code, i see that Guice uses JUL for logging, but i'm using SLF4J so i want to have Guice logging to SLF4J.
With jul-to-slf4j library seems that this is possible, the configuration is this:

public class AppGuiceServletContextListener extends GuiceServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
LogManager.getLogManager().addLogger(Logger.getLogger("com.google.inject")); // This ensure that com.google.inject logger exists
LogManager.getLogManager().getLogger("com.google.inject").addHandler(new SLF4JBridgeHandler()); // Bind SLF4J bridge to "com.google.inject" logger
super.contextInitialized(servletContextEvent);
}

@Override
protected Injector getInjector() {
return Guice.createInjector(...);
}
}


The questions are:
Is this configuration correct?
Is there a better way to do this?
Why not including this in Guice documentation?

Thanks!

Christopher Currie

unread,
Feb 7, 2011, 12:47:20 PM2/7/11
to google...@googlegroups.com
On Mon, Feb 7, 2011 at 9:38 AM, Gian Marco Gherardi
<gianmarco...@gmail.com> wrote:
> Hi, i'm evaluating Guice for use in one of my projects.
> Looking at the code, i see that Guice uses JUL for logging, but i'm using
> SLF4J so i want to have Guice logging to SLF4J.
> With jul-to-slf4j library seems that this is possible, the configuration is
> this:

[snip]

>
> The questions are:
> Is this configuration correct?

Probably not, because you never call the install method on the
SLF4JBridgeHandler. Read the fine documentation at
http://www.slf4j.org/api/org/slf4j/bridge/SLF4JBridgeHandler.html and
bear in mind the performance warning at
http://www.slf4j.org/legacy.html#jul-to-slf4j

> Is there a better way to do this?

Your context listener is an appropriate location to do this, assuming
you are using Guice Servlet as you appear to be.

> Why not including this in Guice documentation?

Because SLF4J is not related to Guice in any way.

Gian Marco Gherardi

unread,
Feb 7, 2011, 1:15:35 PM2/7/11
to google...@googlegroups.com
I've not used SLF4JBridgeHandler.install() because it binds to the root logger, but i only want to redirect Guice logging.
The implementation of the install method is almost the same as the snippet, so this should work as expected (and it works).

> Your context listener is an appropriate location to do this, assuming
> you are using Guice Servlet as you appear to be.

Yes, i'm using it in a Servlet environment.

Because SLF4J is not related to Guice in any way.

I understand that SLF4J is not related to Guice, but i suppose that this is a common scenario when integrating Guice with anything else

Thanks for the help!

Christopher Currie

unread,
Feb 7, 2011, 1:35:30 PM2/7/11
to google...@googlegroups.com
On Mon, Feb 7, 2011 at 10:15 AM, Gian Marco Gherardi
<gianmarco...@gmail.com> wrote:
> I've not used SLF4JBridgeHandler.install() because it binds to the root
> logger, but i only want to redirect Guice logging.
> The implementation of the install method is almost the same as the snippet,
> so this should work as expected (and it works).

Ah, my mistake, I should have read more carefully. In this case, what
you do does work, but I don't think you need the interaction with the
LogManager; the following *should* be sufficient:

Logger.getLogger("com.google.inject").addHandler(new SLF4JBridgeHandler();

The Logger factory method ensures that the logger is registered.

That said, if I were to do this I would not do it in code at all, but
instead use the logging.properties configuration file to install the
handler:

com.google.inject.handlers = org.slf4j.bridge.SLF4JBridgeHandler

IMO this allows you to more easily configure your logging behavior for
different environments. It does assume you want to replace the default
handlers instead of adding to default handlers, as your code does
above.

> I understand that SLF4J is not related to Guice, but i suppose that this is
> a common scenario when integrating Guice with anything else

It is for me, but I think the Guice team is right omit this, because
without co-ordination from SLF4J, their documentation would eventually
not be in sync, and wrong documentation is worse than none. In this
case, there's nothing Guice specific about configuring SLF4J; even the
ContextListener approach is stock Servlets, not specific to Guice at
all.

Good luck!

Gian Marco Gherardi

unread,
Feb 8, 2011, 2:54:12 AM2/8/11
to google...@googlegroups.com
The logging.property approach is definitely a better solution than mine, so i will implement that.

I also agree on your reason to not include SLF4J config on Guice docs.

Thanks a lot for the help!
Reply all
Reply to author
Forward
0 new messages