drill-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Westin <chriswesti...@gmail.com>
Subject Having trouble creating a logger for a generic class?
Date Thu, 05 Mar 2015 00:06:25 GMT
In some files I've been working in, I just noticed that the loggers aren't
static. (This is a different topic than the previous one re making them
private).

I set out to make them private final static, and discovered the reason:
they're generics. There's no way to create class literals for them. That
is, you can't do this:

public class Foo<T> {
  private final static org.slf4j.Logger logger =
org.slf4j.LoggerFactory.getLogger(Foo.class);

Foo<T>.class doesn't work either. Searches reveal that there's no way to
create a class literal
for a generic. However, we are certainly not the only ones struggling with
this, and someone out
there came up with this solution that does work:

public class Foo<T> {
  private final static org.slf4j.Logger logger =
org.slf4j.LoggerFactory.getLogger(new Object()
{}.getClass().getEnclosingClass());

I'll start using this whenever I see the old pattern in a generic class
(which was to not declare the logger static, and to use this.getClass()). I
figure it's better to have some throwaway objects once at class load time
rather than having every instance have it's own (identical) logger.

Chris

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message