0

配置 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 的消息?请如果有人有类似的问题并解决它。

4

2 回答 2

1

我也遇到了 Jboss EAP 6 的问题。我已经解决了。我的工作代码如下:

1.WEB-INF/jboss-deployment-structure.xml文件

<?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>

2. 资源/log4j.properties 文件

# 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
于 2015-05-26T07:08:12.000 回答
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>
于 2014-04-07T10:02:06.903 回答