0

I am initializing the logger in servlet's constructor. Is it a good practice? Should I rather initialize it in a servletContextListener?

Sample Code:

public class MyService{
Logger logger;

 public MyService(){
   logger = Logger.getLogger(MyService.class);
 }

}
rohit
  • 1
  • 2
  • Have a look at: http://stackoverflow.com/questions/3842823/should-logger-be-private-static-or-not – condit Jan 25 '13 at 16:34

2 Answers2

3

Use the logger as a Class level member by declaring it as static

Initialize it properly

Little How-to

private static final Logger logger = Logger.getLogger(*<yourclass>*.class)
Community
  • 1
  • 1
TheWhiteRabbit
  • 15,480
  • 4
  • 33
  • 57
  • @user1348308 You should follow this advice even if using a different logging framework. (Some exceptions may apply of course.) – barfuin Jan 25 '13 at 14:38
0

I think you're talking about initializing the log4j logging system, not a specific logger.

Use servletContextListener is better than the servlet's constructor.

Jintian DENG
  • 3,152
  • 20
  • 22