配置log4j的步骤如下: 步骤1。 创建文件:jboss-deployment-structure.xml
<jboss-deployment-structure>
<deployment>
<exclusions>
<module name="org.apache.log4j" slot="main"/>
<module name="org.apache.commons.logging"/>
</exclusions>
</deployment>
</jboss-deployment-structure>
第2步。 创建servlet:Log4jInitServlet.java
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
public class Log4JInitServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = -3677208571865966932L;
private static final Log log=LogFactory.getLog(Log4JInitServlet.class);
public Log4JInitServlet(){
}
protected void doGet(HttpServletRequest request
,HttpServletResponse response) throws ServletException,IOException{
PrintWriter out = response.getWriter();
out.write("<h1>LogTester Application Version Guide Erasmo Marciano 1.0</h1>");
out.write("<p>Loading this page generates multiple log events for the it.deinformatica.marciano.logtest category.</p>");
out.write("<p>Click on F5 reload this web-page.</p>");
out.write("<p>You wii find level log:debug|fatal|error|trace|info|warn</p>");
out.close();
for (int i = 1; i <= 20; i++) {
log.debug("This is DEBUG message. Event number " + i);
log.fatal("This is FATAL message. Event number " + i);
log.info("This is INFO message. Event number " + i);
log.error("This is ERROR message. Event number " + i);
log.trace("This is TRACE message. Event number " + i);
log.warn("This is WARN message. Event number " + i);
}
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
第3步。 创建文件log4j.properties
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=info, stdout
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
只会显示INFO消息而不显示DEBUG。我究竟做错了什么 或者应该用lo4j DEBUG显示消息? 如果有人有类似的问题并解决了它。
答案 0 :(得分:1)
我也遇到了Jboss EAP 6的问题。我已经解决了。我的工作代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
<deployment>
<exclusions>
<!-- first exclude -->
<module name="javaee.api" />
<module name="org.apache.log4j"/>
<module name="org.slf4j"/>
</exclusions>
<dependencies>
<!-- then include filtered -->
<module name="org.apache.log4j" />
</dependencies>
<exclude-subsystems> <subsystem name="jpa" /> </exclude-subsystems>
</deployment>
</jboss-deployment-structure>
# Root logger option
log4j.rootLogger=INFO, stdout, INF, DBG, ERR
#---------------------------------------------
# Redirect log messages to a log file
#---------------------------------------------
# Output to Tomcat home
logs.dir=${jboss.home}/standalone/log/
logs.fmt.dly=.yyyy-MM-dd
logs.fmt.date=yyyy-MM-dd HH:mm:ss
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# DEBUG Logs
log4j.appender.DBG.Threshold=DEBUG
log4j.appender.DBG.filter=org.apache.log4j.varia.LevelRangeFilter
#log4j.appender.DBG.filter.LevelMin=DEBUG
log4j.appender.DBG.filter.LevelMax=DEBUG
log4j.appender.DBG.filter.AcceptOnMatch=True
log4j.appender.DBG=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DBG.File=${jboss.server.log.dir}/app-debug-log.log
log4j.appender.DBG.DatePattern=${logs.dly.ptrn}
log4j.appender.DBG.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.DBG.layout.ConversionPattern=%d{${logs.fmt.date}} %-5p [%c{1}:%L] - %m%n
# INFO Logs
log4j.appender.INF=org.apache.log4j.DailyRollingFileAppender
log4j.appender.INF.File=${jboss.server.log.dir}/app-info-log.log
log4j.appender.INF.DatePattern=${logs.fmt.dly}
log4j.appender.INF.Threshold=INFO
#log4j.appender.DBG.filter.LevelMin=INFO
log4j.appender.DBG.filter.LevelMax=INFO
log4j.appender.INF.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.INF.layout.ConversionPattern=%d{${logs.fmt.date}} %-5p [%c{1}:%L] - %m%n
# ERROR Logs
log4j.appender.ERR=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ERR.File=${jboss.server.log.dir}/app-err-log.log
log4j.appender.ERR.DatePattern=${logs.fmt.dly}
log4j.appender.ERR.Threshold=ERROR
#log4j.appender.DBG.filter.LevelMin=ERROR
#log4j.appender.DBG.filter.LevelMax=ERROR
log4j.appender.ERR.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.ERR.layout.ConversionPattern=%d{${logs.fmt.date}} %-5p [%c{1}:%L] - %m%n
答案 1 :(得分:0)
尝试排除甚至jboss日志记录,如果你使用它,还可以排除slf4j。<或/> 记住xml中的xmlns,并将该文件放在webapp的WEB-INF文件夹中:
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.1">
<deployment>
<exclusions>
<module name="org.apache.log4j" />
<module name="org.slf4j" />
<module name="org.apache.commons.logging"/>
<module name="org.log4j"/>
<module name="org.jboss.logging"/>
</exclusions>
</deployment>
</jboss-deployment-structure>