无法创建日志文件。
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>log4j-config-location</param-name>
<param-value>/WEB-INF/log4j.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.form</url-pattern>
</servlet-mapping>
</web-app>
我在WEB-INF文件夹中创建了log4j.xml。
的log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true" xmlns:log4j='http://jakarta.apache.org/log4j/'>
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<param name="Threshold" value="debug" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c{1}:%L %m %n" />
</layout>
</appender>
<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="INFO" />
<param name="Threshold" value="debug" />
<param name="maxFileSize" value="20MB" />
<param name="maxBackupIndex" value="10" />
<param name="file" value="${catalina.home}/logs/myLog.log"/>
<param name="append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L %m %n" />
</layout>
</appender>
<!-- 3rdparty Loggers -->
<logger name="org.springframework.core">
<level value="info" />
</logger>
<logger name="org.springframework.beans">
<level value="info" />
</logger>
<logger name="org.springframework.context">
<level value="info" />
</logger>
<logger name="org.springframework.web">
<level value="info" />
</logger>
<!-- Root Logger -->
<root>
<priority value="info"></priority>
<appender-ref ref="fileAppender" />
</root>
</log4j:configuration>
以下是文件夹项目结构:
MyProject
src
web
WEB-INF
lib
web.xml,applicationContext.xml,log4j.xml,spring-config.xml...
任何建议都会有所帮助。我在WEB-INF文件夹中有一个文件,我已经在web.xml中给出了映射,如上所示,但是当tomcat服务器启动时,myLog.log文件仍未创建。我需要添加一些东西吗?我注意到的一件事是Log4jConfigListenner是折旧类。我在类路径中有log4j-1.2.17.jar文件。我不想将日志文件保存在src文件夹中,我希望它在WEB-INF文件夹中。
- edit-- 我已经在web.xml中修改了侦听器声明的顺序,但仍然没有生成日志文件。
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
答案 0 :(得分:0)
检查web.xml中侦听器的顺序 在Log4jConfigListener上指定了javadoc:
使用自定义log4j初始化时,应在web.xml中的ContextLoaderListener之前注册此侦听器。
答案 1 :(得分:-1)
您是否尝试过调用类中的logger并运行该类? 你能尝试使用JAVA logger Utils类,如::
public class LoggerUtils {
private static final Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
private static LoggerUtils LOGGER = null;
private static final int MAX_LOGFILE_SIZE_BYTES = 1024 * 1024 * 5; // 5MB
public static Logger getInstance() {
if(LOGGER == null) {
try {
initialize();
LOGGER = new LoggerUtils();
} catch (IOException e) {
e.printStackTrace();
}
}
return logger;
}
private static void initialize() throws IOException {
// Suppress the logging output to the console
Logger rootLogger = Logger.getLogger("");
Handler[] handlers = rootLogger.getHandlers();
if (handlers.length > 0 && handlers[0] instanceof ConsoleHandler) {
rootLogger.removeHandler(handlers[0]);
}
logger.setLevel(Level.ALL);
// Get the .log file path
String path = Platform.getLogFileLocation().toOSString();
FileHandler fileTxt = new FileHandler(path, MAX_LOGFILE_SIZE_BYTES, 1);
FileHandler fileHTML = new FileHandler(path + ".html");
// create a text format
SimpleFormatter formatterTxt = new SimpleFormatter();
fileTxt.setFormatter(formatterTxt);
logger.addHandler(fileTxt);
// create an HTML format
LoggerHtmlFormatter formatterHTML = new LoggerHtmlFormatter();
fileHTML.setFormatter(formatterHTML);
logger.addHandler(fileHTML);
}
}
class LoggerHtmlFormatter extends Formatter {
public String format(LogRecord rec) {
StringBuffer buf = new StringBuffer(1000);
buf.append("<tr>\n");
if (rec.getLevel().intValue() >= Level.WARNING.intValue()) {
buf.append("\t<td style=\"color:red\">");
buf.append("<b>");
buf.append(rec.getLevel());
buf.append("</b>");
} else {
buf.append("\t<td>");
buf.append(rec.getLevel());
}
buf.append("</td>\n");
buf.append("\t<td>");
buf.append(calcDate(rec.getMillis()));
buf.append("</td>\n");
buf.append("\t<td>");
buf.append(formatMessage(rec));
buf.append("</td>\n");
buf.append("</tr>\n");
return buf.toString();
}
private String calcDate(long millisecs) {
SimpleDateFormat date_format = new SimpleDateFormat("MMM dd,yyyy HH:mm");
Date resultdate = new Date(millisecs);
return date_format.format(resultdate);
}
public String getHead(Handler h) {
return "<!DOCTYPE html>\n<head>\n<style>\n"
+ "table { width: 100% }\n"
+ "th { font:bold 10pt Tahoma; }\n"
+ "td { font:normal 10pt Tahoma; }\n"
+ "h1 {font:normal 11pt Tahoma;}\n"
+ "</style>\n"
+ "</head>\n"
+ "<body>\n"
+ "<h1>" + (new Date()) + "</h1>\n"
+ "<table border=\"0\" cellpadding=\"5\" cellspacing=\"3\">\n"
+ "<tr align=\"left\">\n"
+ "\t<th style=\"width:10%\">Loglevel</th>\n"
+ "\t<th style=\"width:15%\">Time</th>\n"
+ "\t<th style=\"width:75%\">Log Message</th>\n"
+ "</tr>\n";
}
public String getTail(Handler h) {
return "</table>\n</body>\n</html>";
}
}
请注意,你需要从一些java类中调用它,比如LoggerUtils.getInstance(level.SEVERE,&#34; ANYTHING&#34;);并运行该类。记录器仅在被调用时生成,或者在打印一些日志之前生成,之后它甚至不创建文件。