简体   繁体   中英

Where should a logger(log4j) be initialized in a servlet?

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);
 }

}

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)

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

Use servletContextListener is better than the servlet's constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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