使用 JBoss 7、Java 6。
我的 log4j.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- general application log -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<param name="Threshold" value="info"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1} --> %m%n"/>
</layout>
</appender>
<!-- additional logging -->
<appender name="util" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="${user.home}/bs/logs/login.log"/>
<param name="MaxFileSize" value="100KB"/>
<param name="MaxBackupIndex" value="100"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss} %m%n"/>
</layout>
</appender>
<logger name="ru.bs.web" additivity="false">
<appender-ref ref="console"/>
</logger>
<logger name="ru.bs.web.security">
<appender-ref ref="util"/>
</logger>
<root>
<level value="INFO"/>
</root>
</log4j:configuration>
问题 :
我想通过 log4j 记录来自 ru.bs.web 包的所有应用程序使用控制台附加程序,并从文件中的 ru.bs.web.security 记录。
如何从控制台附加程序中排除包 ru.bs.web.security ?
我正在使用如何使用 log4j.xml 配置 log4j 以根据类名附加到不同的日志文件中的配置?但它不起作用。
我解决了!
我的 XML 配置在这里:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="false" xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- general application log -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<param name="Threshold" value="info"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1} --> %m%n"/>
</layout>
</appender>
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="${user.home}/bs/logs/login.log"/>
<param name="MaxFileSize" value="100MB"/>
<param name="MaxBackupIndex" value="10000"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss} %m%n"/>
</layout>
</appender>
<logger name="ru.bs.web.security" additivity="false">
<level value="LOGIN" class="ru.bs.web.util.LoginLevel"/>
<appender-ref ref="file"/>
</logger>
<root>
<level value="info"/>
<appender-ref ref="console"/>
<appender-ref ref="file"/>
</root>
</log4j:configuration>
和 ru.bs.web.util.LoginLevel 类:
public class LoginLevel extends Level {
private static final long serialVersionUID = 1L;
public static final int LOGIN_INT = INFO_INT + 100 ;
public static final Level LOGIN = new LoginLevel(LOGIN_INT,"LOGIN",6);
protected LoginLevel(int level, String levelStr, int syslogEquivalent) {
super(level, levelStr, syslogEquivalent);
}
}
运行它 - log.log(LoginLevel.LOGIN, "from LOGGER!");