简体   繁体   English

防止在SessionScoped Bean中序列化CDI注入的Logger

[英]Prevent serialization of CDI injected Logger in SessionScoped beans

Since I started to use XRebel, I was wondering about the following: 自从我开始使用XRebel以来,我一直在想以下几点:

We started replacing our logger (SLF4J) fields from: 我们开始从以下位置替换记录器(SLF4J)字段:

private static final Logger log = LoggerFactory...;

to

@Inject
private Logger log;

with a respective @Produces producer. 与各自的@Produces生产者。

This works fine in general, but I was wondering about the size of @SessionScoped beans. 通常,这可以正常工作,但是我想知道@SessionScoped bean的大小。 They now always have an own logger, adding - according to XRebel - about 900k to every single one of the beans. 他们现在始终拥有自己的记录器,根据XRebel的说法,每个Bean大约增加了900k。

Now, the SLF4J LoggerFactory.getLogger(Class clazz) does, according to the docs , 根据文档 ,现在SLF4J LoggerFactory.getLogger(Class clazz) 可以了

Return a logger named corresponding to the class passed as parameter, using the statically bound ILoggerFactory instance 使用静态绑定的ILoggerFactory实例,返回与作为参数传递的类相对应的命名记录器

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? 所以我的问题是:容器在每个会话bean的每个实例中是否确实都有一个记录器,从而在会话大小上产生了相当大的开销,还是使用@Inject变体而不产生所有开销是安全的?

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. 您可以使用应用程序范围的记录器,这将大大减少开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM