Java Log4j未写入日志文件
我第一次尝试使用ServletContext使用Java Web应用程序登录。我没有得到任何日志文件。有人能告诉我什么地方我做得不对吗 我的ServletContextListener:Java Log4j未写入日志文件,java,log4j,Java,Log4j,我第一次尝试使用ServletContext使用Java Web应用程序登录。我没有得到任何日志文件。有人能告诉我什么地方我做得不对吗 我的ServletContextListener: @Override public void contextInitialized(ServletContextEvent sce) { ServletContext context = sce.getServletContext(); String prefix = c
@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext context = sce.getServletContext();
String prefix = context.getRealPath("/");
String file = "WEB-INF" + System.getProperty("file.separator") + "classes" + System.getProperty("file.separator") + "log4j.properties";
if (file != null) {
PropertyConfigurator.configure(prefix + file);
System.out.println("Log4J Logging started for application: " + prefix + file);
} else {
System.out.println("Log4J Is not configured for application Application: " + prefix + file);
}
}
My Web.xml:
<listener>
<listener-class>com.kollega.listener.KollegaContextListener</listener-class>
</listener>
我的测试Servlet:
private final Logger log = Logger.getLogger("kollegalogger");
log.debug(">>> Connectition TO database Successful for Login");
Logger.getLogger(LoginServlet.class.getName()).log(Level.FATAL, ">>>>Connectition TO database Successful for Login");
我只获取调试输出,但WEB-INF/logs/kollega.Log中没有日志文件。我还缺什么吗
编辑:
*我从文件路径中删除了${ApprotPath},并且正在C:/WEB-INF/logs/kollega.Log中创建日志。我需要它在我的WEB-INF目录中*试试这个。web应用程序中的路径相对于
/web-INF
而不是web-INF
String file = System.getProperty("file.separator")+"WEB-INF" + System.getProperty("file.separator") + "classes" + System.getProperty("file.separator") + "log4j.properties";
将属性文件放在类文件所在的类路径中,或放在
WEB-INF/classes
中,然后您可以按如下方式轻松加载它:
PropertyConfigurator.configure("log4j.properties");**
我在WEB-INF文件夹中仍然没有任何日志文件。我正在使用Netbeans(/WEB-INF是您的WEB应用程序中的上下文文件夹。您是否告诉我文件不可能写入该文件夹?如果这样,我如何才能将其写入WEB-INF文件夹之外的应用程序文件夹内?当然,日志可以写入应用程序(或服务器)的文件夹之外但在上述情况下,真正的问题是应用程序无法定位log4j.properties文件。这就是为什么它要用C:/WEB-INF???编写日志文件,并且我从调试
log4j日志中得到这个输出,该日志是为应用程序启动的:C:\Users\User\Documents\NetBeansProjects\Kollega\build\WEB\WEB-INF\classes\log4j.properties
,所以我认为可以找到e属性文件。
PropertyConfigurator.configure("log4j.properties");**