3

myController.java

 private static final Logger log = Logger.getLogger(myController.class
  .getName());
@GET
  @Path("/testDebug")
  public String testDebug(@Context final ServletContext context)
  {
    log.error("This is an error message");
    log.debug("This is a debug message");
    log.fatal("This is fatal message");
    log.warn("This is a warn message");
    log.info("This is a info message");

    return "Test Page.  Debug Mode is on =" + log.isDebugEnabled();
  }

jboss-log4j.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<!-- ===================================================================== -->
<!--                                                                       -->
<!--  Log4j Configuration                                                  -->
<!--                                                                       -->
<!-- ===================================================================== -->

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">

   <!-- ================================= -->
   <!-- Preserve messages in a local file -->
   <!-- ================================= -->

   <!-- A time/date based rolling appender -->
   <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="false"/>

      <!-- Rollover at midnight each day -->
      <param name="DatePattern" value="'.'yyyy-MM-dd"/>

      <layout class="org.apache.log4j.PatternLayout">
         <!-- The default pattern: Date Priority [Category] Message\n -->
         <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>

          -->
      </layout>
   </appender>

   <!-- ============================== -->
   <!-- Append messages to the console -->
   <!-- ============================== -->

   <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">
         <!-- The default pattern: Date Priority [Category] Message\n -->
         <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
      </layout>
   </appender>


   <!-- ================ -->
   <!-- Limit categories -->
   <!-- ================ -->

   <!-- Limit the org.apache category to INFO as its DEBUG is verbose -->
   <category name="org.apache">
      <priority value="INFO"/>
   </category>

   <!-- Limit the org.jboss.serial (jboss-serialization) to INFO as its DEBUG is verbose -->
   <category name="org.jboss.serial">
      <priority value="INFO"/>
   </category>

   <!-- Limit the org.jgroups category to WARN as its INFO is verbose -->
   <category name="org.jgroups">
      <priority value="WARN"/>
   </category>

   <!-- Limit the jacorb category to WARN as its INFO is verbose -->
   <category name="jacorb">
      <priority value="WARN"/>
   </category>

   <!-- Limit JBoss categories -->
   <category name="org.jboss">
      <priority value="INFO"/>
      <appender-ref ref="CONSOLE"/>
   </category>



   <!-- Limit the JSR77 categories -->
   <category name="org.jboss.management">
      <priority value="INFO"/>
   </category>

   <!-- This is is the package to myController.java -->
   <category name="com.myPackage.src">
      <priority value="DEBUG"/>
   </category>

   <!-- ======================= -->
   <!-- Setup the Root category -->
   <!-- ======================= -->

   <root>
      <appender-ref ref="CONSOLE"/>
      <appender-ref ref="FILE"/>
   </root>

</log4j:configuration>

期望的结果

当路径/testDebug被命中时得到消息:“这是一条调试消息”

问题:

除了调试,我得到所有要显示的消息

10:29:49,440 INFO  [STDOUT] 10:29:49,440 ERROR [myController] This is an error message
10:29:49,440 INFO  [STDOUT] 10:29:49,440 FATAL [myController] This is fatal message
10:29:49,440 INFO  [STDOUT] 10:29:49,440 WARN  [myController] This is a warn message
10:29:49,440 INFO  [STDOUT] 10:29:49,440 INFO  [myController] This is a info message

为什么我无法显示调试消息?

4

4 回答 4

5

有关查看 Jboss 7.1.1+ 的调试日志的更新信息,请参阅此问题。

几点:

  1. 您的班级myController按预期使用日志记录。(另一个答案声称您错误地实例化了您的记录器;不是真的。

  2. 从您的项目中删除 log4j.xml(或 logback.xml 等)配置文件(即,从main/resources/META-INF

  3. 在您的pom.xml中,确保您的slf4j依赖项具有提供的范围 Jboss 将提供此)。此外,不要包含 log4j(或 logback 等)实现依赖项。例如,您的日志记录依赖项部分可能如下所示:

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.7</version>
        <scope>provided</scope>
    </dependency>
    
  4. 确保您的文件中有您的包的记录器standalone/configuration/standalone.xml条目。

    <logger category="com.myPackage.src">
         <level name="DEBUG"/>
    </logger>
    
  5. 确保控制台处理程序周期性旋转文件处理程序处理程序具有调试级别。(默认情况下,它们是 INFO。它们需要是 DEBUG 否则您的调试语句将被忽略。)

请注意,有一个 CLI 允许您更新日志记录。我手动完成,但使用 CLI 可能是明智的。

这是您的standalone.xml日志记录部分的缩写版本:

    <subsystem xmlns="urn:jboss:domain:logging:2.0">
        <console-handler name="CONSOLE">
            <level name="DEBUG"/>
            <formatter>
                <named-formatter name="COLOR-PATTERN"/>
            </formatter>
        </console-handler>
        <periodic-rotating-file-handler name="FILE" autoflush="true">
            <level name="DEBUG"/>
            <formatter>
                <named-formatter name="PATTERN"/>
            </formatter>
            <file relative-to="jboss.server.log.dir" path="server.log"/>
            <suffix value=".yyyy-MM-dd"/>
            <append value="true"/>
        </periodic-rotating-file-handler>
        <logger category="com.myPackage.src">
            <level name="DEBUG"/>
        </logger>
        <root-logger>
            <level name="INFO"/>
            <handlers>
                <handler name="CONSOLE"/>
                <handler name="FILE"/>
            </handlers>
        </root-logger>
        <formatter name="PATTERN">
            <pattern-formatter pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>
        </formatter>
        <formatter name="COLOR-PATTERN">
            <pattern-formatter pattern="%K{level}%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>
        </formatter>
    </subsystem>
于 2015-01-14T03:55:49.520 回答
0

您的类别定义不正确。当您实例化您正在使用的记录器时,Class.getName()它会返回完全限定的类名。您只使用包名称定义您的记录器类别,这意味着没有为该类别打开调试com.myPackage.src.myController

您需要更改实例化记录器的方式

private static final Logger log = Logger.getLogger(myController.class.getPackage().getName());

或更改您的类别定义

<category name="com.myPackage.src.myController">
  <priority value="DEBUG"/>
</category>

另一方面,包不应该有大写字符,类名应该总是以大写字母开头。

于 2012-06-22T17:20:23.133 回答
0

您可以尝试这样做吗:

<category name="com.myPackage.src">
      <priority value="DEBUG"/>
   </category>
于 2012-06-20T15:41:43.970 回答
-1
Detailed information could be found at: https://docs.jboss.org/author/display/AS71/Logging+Configuration
<subsystem xmlns="urn:jboss:domain:logging:1.0">
   <console-handler name="CONSOLE" autoflush="true">
       <level name="DEBUG"/>
       <formatter>
           <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>
       </formatter>
   </console-handler>
   <periodic-rotating-file-handler name="FILE" autoflush="true">
       <formatter>
           <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/>
       </formatter>
       <file relative-to="jboss.server.log.dir" path="server.log"/>
       <suffix value=".yyyy-MM-dd"/>
   </periodic-rotating-file-handler>
   <logger category="com.arjuna">
       <level name="WARN"/>
   </logger>
   [...]
   <root-logger>
       <level name="DEBUG"/>
       <handlers>
           <handler name="CONSOLE"/>
           <handler name="FILE"/>
       </handlers>
   </root-logger>
</subsystem>

1.可以在standalone.xml中进行更改2.上面的解决方案是显示如何在jboss中查看日志的调试消息这将解决上述问题

于 2020-03-03T08:13:19.490 回答