1

Since I started to use XRebel, I was wondering about the following:

We started replacing our logger (SLF4J) fields from:

private static final Logger log = LoggerFactory...;

to

@Inject
private Logger log;

with a respective @Produces producer.

This works fine in general, but I was wondering about the size of @SessionScoped beans. They now always have an own logger, adding - according to XRebel - about 900k to every single one of the beans.

Now, the SLF4J LoggerFactory.getLogger(Class clazz) does, according to the docs,

Return a logger named corresponding to the class passed as parameter, using the statically bound ILoggerFactory instance

But I am not exactly sure, how this plays together.

So my question is: does the container really have one logger in every instance of every session bean, producing quite some overhead in session size, or is it safe to use the @Inject variant without producing all that overhead?

Dominik Sandjaja
  • 6,326
  • 6
  • 52
  • 77
  • 1
    I'm curious, why are you doing this? What benefit are you getting from injecting a logger? – John Ament May 14 '15 at 17:35
  • @JohnAment well, it was not exactly my idea, I'd still stick to that `private static final` approach, but - and I quote - "because those CDI loggers make the code so much more readable" this is what we have now. Anyway, I am still not sure if the CDI approach produces more session overhead or not ... – Dominik Sandjaja May 15 '15 at 06:38
  • BTW, can you clarify if your producer method declares a scope. – John Ament May 15 '15 at 22:41
  • @JohnAment the Producer method does not declare a scope: `@Produces public static Logger getLogger(InjectionPoint injectionPoint) { return LoggerFactory.getLogger(injectionPoint.getMember().getDeclaringClass());}` I cannot annotate the method with `@ApplicationScoped`, Eclipse gives me some error: Bean that declares any scope other than @Dependent has an injection point of type InjectionPoint and qualifier @Default [JSR-299 §5.5.7] – Dominik Sandjaja May 18 '15 at 11:34
  • @JohnAment: might an injected logger be helpful to allow the Logger to automatically include session information on every log entry (Eg., formatting the log entry to include the user's id, or present size of shopping cart, etc.)? – mwhidden Mar 24 '16 at 16:49
  • @mwhidden I don't think so, are you referring to MDC type variables? – John Ament Mar 25 '16 at 02:42
  • @JohnAment I'm new to Spring and not sure what MDC type variables are, but I had success using a session-scoped custom logger auto-wired into `@Controller` classes for adding session data to log entries. – mwhidden Mar 29 '16 at 21:43

1 Answers1

2

Ok to answer your question - it depends.

If you scope your logger to be session scoped then yes, each session will have its own logger. That logger will need to be serializable (and they typically aren't, since they have file handles behind them).

What I've done - besides having my own logger facade - is to make them application scoped. Typically behinds the scenes loggers are highly synchronized (multiple callers writing to a single appender), changes are queued up and written periodically. You can use an application scoped logger, which will reduce the over head dramatically.

John Ament
  • 11,595
  • 1
  • 36
  • 45