我正在尝试使用 log4php 并且在理解配置时遇到问题,我正在比较从 log4j xml 到相同的 log4php xml 的行为。相同的代码。但是输出不同。从文档中,我希望记录 5 行,但在 log4php 中,只有一个。拜托,我快把自己逼疯了。
PHP代码:
<?php
include("log4php/Logger.php");
Logger::configure('log4php.xml');
$rlogger = Logger::getRootLogger();
$rlogger->debug('Not logged');
$rlogger->error('Logged');
$logger = Logger::getLogger('com.suri');
$logger->debug('Logged');
$logger->warn('Logged');
$logger = Logger::getLogger('com.suri.factory');
$logger->debug('Not logged');
$logger->warn('Logged');
$logger = Logger::getLogger('com.suri.factory.Bar');
$logger->debug('Not logged');
$logger->info('Logged');
?>
XML配置:
<?xml version="1.0" encoding="UTF-8" ?>
<configuration xmlns="http://logging.apache.org/log4php/">
<!-- A1 is set to be a ConsoleAppender -->
<appender name="A1" class="LoggerAppenderConsole">
<layout class="LoggerLayoutPattern">
<param name="ConversionPattern" value="%d [%t] - %-5p %c - %m%n"/>
</layout>
</appender>
<logger name="com.suri.factory.Bar">
<level value="info"/>
</logger>
<logger name="com.suri.factory">
<level value="warn"/>
</logger>
<logger name="com">
<level value="debug"/>
</logger>
<root>
<level value="error" />
<appender-ref ref="A1" />
</root>
</configuration>
输出:
2014-03-13T18:01:30-03:00 [14939] - ERROR root - Logged
预期输出(在 jython+log4j 中使用相同的配置和相同的代码生成):
2014-03-13 18:09:03,591 [main] - ERROR root - Logged
2014-03-13 18:09:03,592 [main] - DEBUG com.suri - Logged
2014-03-13 18:09:03,592 [main] - WARN com.suri - Logged
2014-03-13 18:09:03,592 [main] - WARN com.suri.factory - Logged
2014-03-13 18:09:03,592 [main] - INFO com.suri.factory.Bar - Logged
jython代码:
from org.apache.log4j import *
from org.apache.log4j.xml import *
if __name__ == '__main__':
xml.DOMConfigurator.configure('log4j.xml')
rlogger = LogManager.getRootLogger()
rlogger.debug('Not logged');
rlogger.error('Logged');
logger = LogManager.getLogger('com.suri');
logger.debug('Logged');
logger.warn('Logged');
logger = LogManager.getLogger('com.suri.factory');
logger.debug('Not logged');
logger.warn('Logged');
logger = LogManager.getLogger('com.suri.factory.Bar');
logger.debug('Not logged');
logger.info('Logged');
Jython XML 配置:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- A1 is set to be a ConsoleAppender -->
<appender name="A1" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] - %-5p %c - %m%n"/>
</layout>
</appender>
<logger name="com.suri.factory.Bar">
<level value="info"/>
</logger>
<logger name="com.suri.factory">
<level value="warn"/>
</logger>
<logger name="com">
<level value="debug"/>
</logger>
<root>
<level value="error" />
<appender-ref ref="A1" />
</root>
</log4j:configuration>
谢谢!