我有 log4php 配置,其中包含特定文件中每个级别的附加程序,如下所示:
<configuration xmlns="http://logging.apache.org/log4php/">
<appender name="info" class="LoggerAppenderRollingFile">
<layout class="LoggerLayoutPattern">
<param name="conversionPattern" value="%date %logger %-5level %msg%n"/>
</layout>
<param name="file" value="../app/logs/info.log"/>
<param name="append" value="true"/>
<param name="maxFileSize" value="100MB"/>
<param name="maxBackupIndex" value="1"/>
<filter class="LoggerFilterLevelRange">
<param name="levelMin" value="info"/>
<param name="levelMax" value="info"/>
</filter>
</appender>
<appender name="warn" class="LoggerAppenderRollingFile">
<layout class="LoggerLayoutPattern">
<param name="conversionPattern" value="%date %logger %-5level %msg%n"/>
</layout>
<param name="file" value="../app/logs/warn.log"/>
<param name="append" value="true"/>
<param name="maxFileSize" value="100MB"/>
<param name="maxBackupIndex" value="1"/>
<filter class="LoggerFilterLevelRange">
<param name="levelMin" value="warn"/>
<param name="levelMax" value="warn"/>
</filter>
</appender>
<appender name="error" threshold="error" class="LoggerAppenderRollingFile">
<layout class="LoggerLayoutPattern">
<param name="conversionPattern" value="%date %logger %-5level %msg%n"/>
</layout>
<param name="file" value="../app/logs/error.log"/>
<param name="append" value="true"/>
<param name="maxFileSize" value="100MB"/>
<param name="maxBackupIndex" value="1"/>
<filter class="LoggerFilterLevelRange">
<param name="levelMin" value="error"/>
<param name="levelMax" value="fatal"/>
</filter>
</appender>
<appender name="debug" class="LoggerAppenderRollingFile">
<layout class="LoggerLayoutPattern">
<param name="conversionPattern" value="%date %logger %-5level %msg%n"/>
</layout>
<param name="file" value="../app/logs/debug.log"/>
<param name="append" value="true"/>
<param name="maxFileSize" value="100MB"/>
<param name="maxBackupIndex" value="1"/>
<filter class="LoggerFilterLevelRange">
<param name="levelMin" value="debug"/>
<param name="levelMax" value="debug"/>
</filter>
</appender>
<root>
<level value="debug" />
<appender-ref ref="info"/>
<appender-ref ref="warn"/>
<appender-ref ref="error"/>
<appender-ref ref="debug"/>
</root>
</configuration>
以及以下 php 代码:
Logger::configure('config.xml');
$logger = Logger::getRootLogger();
$logger->warn("test");
$logger->error("test");
$logger->info("test");
$logger->debug("test");
当我运行 php 代码时,会创建每个文件并按预期为每个文件逐级写入日志,但是每个日志行都像这样重复:
cat logs/info.log
2017-10-01T22:06:44+00:00 root INFO test
2017-10-01T22:06:44+00:00 root INFO test
我究竟做错了什么?
更新
还尝试了 Monolog 记录器,但有同样的问题,看起来类加载有问题。我正在使用作曲家,它的配置有下一行:
{
"require": {
"monolog/monolog": "1.23.0"
},
"autoload": {
"psr-4": {
"App\\": "src"
}
}
}
而且我有 htaccess,我将流量重定向到索引 php,在那里我初始化 App 类(不在 src 中)。在那个类中,我只是调用下一个代码:
$this->logger = new Logger('default');
$this->logger->pushHandler(new StreamHandler(LOGS_DIR . 'info.log', Logger::INFO));
$this->logger->pushHandler(new StreamHandler(LOGS_DIR . 'error.log', Logger::ERROR));
$this->logger->pushHandler(new StreamHandler(LOGS_DIR . 'debug.log', Logger::DEBUG));
$this->logger->error("error");
$this->logger->info("info");
$this->logger->debug("debug");
我使用作曲家的方式可能有问题吗?