Can I use instance variables in a session without bean state?

I know that the idle bean does not support dialog state, but I only need a logger. Should I get a logger in every method that is called? If not, where should I initialize it?

Is it for sure that if I write such code, I will not get a NullPointerException in some method that uses logger?

 @PostConstruct
 public void init() {
   logger = Logger.getLogger();
 }
+3
source share
3 answers

I assume that you do not want to have a request / session dependent registrar, right? In this case, you could even use a static member of the class to add a registrar and allow all bean instances to use the same log.

+1
source

, :

  • , ,
  • getInstance(),
  • getLogger()


getLogger() , .

0

Try:

private static final Logger log = Logger.getLogger();

If you need Thread-specific values, you should ensure that the Logger output handler writes the Thread name on each line, and you should be fine.

0
source

All Articles