简体   繁体   English

在Servlet中应将记录器(log4j)初始化在哪里?

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

I am initializing the logger in servlet's constructor. 我正在Servlet的构造函数中初始化记录器。 Is it a good practice? 这是一个好习惯吗? Should I rather initialize it in a servletContextListener? 我应该在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. 我认为您是在谈论初始化log4j日志记录系统,而不是特定的记录器。

Use servletContextListener is better than the servlet's constructor. 使用ServletContextListener优于Servlet的构造函数。

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

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