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
被命中时,会收到消息:“这是一条调试消息”
问题:
我收到所有消息显示, debug
除外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
为什么我无法显示调试信息?
答案 0 :(得分:4)
有关使用Jboss 7.1.1 +查看调试日志记录的更新信息,请参阅this question。
有些观点:
您的班级myController
按预期使用日志记录。 (另一个答案声称您错误地实例化了记录器;不是真的。)
从项目中删除log4j.xml(或logback.xml等)配置文件(即,从main/resources/META-INF
)
在pom.xml
中,确保您的slf4j
依赖项具有提供的范围(Jboss将提供此功能)。此外,不要包含log4j(或logback等)实现依赖项。例如,这是您的日志记录依赖项部分的样子:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
<scope>provided</scope>
</dependency>
确保您的standalone/configuration/standalone.xml
文件中包含记录器条目。
<logger category="com.myPackage.src">
<level name="DEBUG"/>
</logger>
确保 console-handler 和 periodic-rotating-file-handler 处理程序具有DEBUG级别。 (默认情况下,它们是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>
答案 1 :(得分:0)
你能尝试这样做吗?
<category name="com.myPackage.src">
<priority value="DEBUG"/>
</category>
答案 2 :(得分: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>
一方面,包不应该包含大写字符,类名应该始终以大写字母开头。
答案 3 :(得分:-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中日志的调试消息 这将解决以上问题