Logging with DEBUG level in JBoss 7.1.1

青春壹個敷衍的年華 提交于 2019-12-07 18:21:55

问题


Goal: My application should have messages with ERROR and DEBUG levels. Level of logging must set (switch) via JBoss Admin Console. Logging should be written to standard JBoss log file and server console.

I tried to use java.util.logging.Logger, but this logger have not necessary levels. I switched to log4j with slf4j. Messages with ERROR level is exist. Problem with DEBUG and System.out.println. Interesting, that DEBUG level is visible in testing phase.

 import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    /**
     * Logger creation and configuration
     */
    public class ResourcesLog {

        @Produces
        Logger getLog(InjectionPoint ip) {
            String category = ip.getMember().getDeclaringClass().getName();
            return LoggerFactory.getLogger(category);
        }
    }

or just LOG = LoggerFactory.getLogger(MyClass.class); Pom file:

<dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.6</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.6</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.6</version>

        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.6</version>
        </dependency>

Maybe contains unnecessary. I can't set slf4j-simple and slf4j-api as "provided" - got an error. Should this libs be inside war?

jboss-deployment-structure.xml in WEb-INF:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
    <deployment>
        <exclusions>
            <module name="org.apache.commons.logging" />
            <module name="org.apache.log4j" />
            <module name="org.jboss.logging" />
            <module name="org.jboss.logging.jul-to-slf4j-stub" />
            <module name="org.jboss.logmanager" />
            <module name="org.jboss.logmanager.log4j" />
            <module name="org.slf4j" />
            <module name="org.slf4j.impl" />
        </exclusions>
    </deployment>
</jboss-deployment-structure>

And log4j.xml settings in resource folder (in war is in 'WEB-INF/classes/log4j.xml'):

<?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
    <appender name="FILE" class="org.jboss.logging.appender.DailyRollingFileAppender">
        <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
        <param name="File" value="${jboss.server.log.dir}/server.log"/>
        <param name="Append" value="true"/>
        <param name="DatePattern" value="'.'yyyy-MM-dd"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
        </layout>
    </appender>
    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
        <param name="Target" value="System.out"/>
        <param name="Threshold" value="DEBUG"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
        </layout>
    </appender>
    <category name="org.mypackage">
        <priority value="DEBUG"/>
    </category>
    <root>
        <level value="INFO"/>
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="FILE"/>
    </root>
</log4j:configuration>

回答1:


Since you want your logs to go to the standard logging configuration and you want to control this configuration with a management option, there is no need to use log4j. Ditch the log4j.xml and the jboss-deployment-structure.xml. Also you'll only need the slf4j-api and it needs to be marked as provided.

Next you need to configure a logger on the server. In CLI it would be a command like the following for a standalone server

/subsystem=logging/logger=org.mypackage:add(level=DEBUG)

You'd also need to set the console handler and the file handler to allow for debug messages. By default the file handler is already set to print debug messages. To change the console handler to allow debug messages execute the follow CLI command.

/subsystem=logging/console-handler=CONSOLE:write-attribute(name=level,value=DEBUG)

These changes can also be made in the web console as well.



来源:https://stackoverflow.com/questions/22044656/logging-with-debug-level-in-jboss-7-1-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!