Log4php的levelRange单独输出不起作用

时间:2018-07-05 10:25:16

标签: php apache configuration log4php

我在配置log4php时遇到问题,我不知道在哪里查看文档。我有这个配置:

Logger::configure(array(
    'rootLogger' => array(
        'level' => 'INFO',
        'appenders' => array('info'),
    ),
    'loggers' => array(
        'debug' => array(
            'level' => 'DEBUG',
            'appenders' => array('debug'),
            'additivity' => false
        ),
        'error' => array(
            'level' => 'ERROR',
            'appenders' => array('error'),
            'additivity' => false
        )
    ),
    'appenders' => array(
        'info' => array(
            'class' => 'LoggerAppenderDailyFile',
            'layout' => array(
                'class' => 'LoggerLayoutPattern',
                'params' => array(
                    'conversionPattern' => '%date %logger %-5level %msg%n'
                )
            ),
            'params' => array(
                'datePattern' => 'Y-m-d',
                'file' => '../log/ilias-info-%s.log',
                'append' => true
            ),
            'filters' => array(
                    'class' => 'LoggerFilterLevelRange',
                    'params' => array(
                        'levelMin' => 'info',
                        'levelMax' => 'info',
                    )
            )
        ),
        'debug' => array(
            'class' => 'LoggerAppenderDailyFile',
            'layout' => array(
                'class' => 'LoggerLayoutPattern',
                'params' => array(
                    'conversionPattern' => '%date %logger %-5level %msg%n'
                )
            ),
            'params' => array(
                'datePattern' => 'Y-m-d',
                'file' => '../log/ilias-debug-%s.log',
                'append' => true
            ),
            'filters' => array(
                    'class' => 'LoggerFilterLevelRange',
                    'params' => array(
                        'levelMin' => 'debug',
                        'levelMax' => 'debug',
                    )
            )
        ),
        'error' => array(
            'class' => 'LoggerAppenderDailyFile',
            'layout' => array(
                'class' => 'LoggerLayoutPattern',
                'params' => array(
                    'conversionPattern' => '%date %logger %-5level %msg%n'
                )
            ),
            'params' => array(
                'datePattern' => 'Y-m-d',
                'file' => '../log/ilias-error-%s.log',
                'append' => true
            ),
            'filters' => array(
                    'class' => 'LoggerFilterLevelRange',
                    'params' => array(
                        'levelMin' => 'error',
                        'levelMax' => 'error',
                    )
            )
        )
    )
));

用法: 我曾经定义

$logger = Logger::getLogger(basename(__FILE__));

然后我这样记录我的需求:

$logger->info("INFO");
$logger->debug("DEBUG");
$logger->error("ERROR");

,但是仅创建信息的日志文件。我使用PHP方式配置记录器,因为对我而言,这似乎是最简单的方式,但是关于这种配置方式的文档并不多。我在做什么错了?

1 个答案:

答案 0 :(得分:0)

好吧,我也花了一段时间才能使其正常工作。我当前的配置有效:

<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns="http://logging.apache.org/log4php/">
<appender name="default" class="LoggerAppenderRollingFile" threshold="DEBUG">
    <layout class="LoggerLayoutPattern">
        <param name="conversionPattern" value="%date %-30logger %-5level %msg%n"/>
    </layout>
    <param name="file" value="/mnt/media/log/backend.debug.log"/>
    <param name="maxFileSize" value="100MB"/>
    <param name="maxBackupIndex" value="20"/>
</appender>
<appender name="splunk" class="LoggerAppenderRollingFile" threshold="INFO">
    <layout class="LoggerLayoutPattern">
        <param name="conversionPattern" value="%date %-30logger %-5level %msg%n"/>
    </layout>
    <param name="file" value="/mnt/media/log/backend.log"/>
    <param name="maxFileSize" value="100MB"/>
    <param name="maxBackupIndex" value="20"/>
</appender>
<appender name="console" class="LoggerAppenderConsole" threshold="DEBUG">
    <layout class="LoggerLayoutSimple">
    </layout>
</appender>

<root>
    <level value="TRACE"/>
    <appender_ref ref="default"/>
    <appender_ref ref="splunk"/>
    <appender_ref ref="console"/>
</root>

要使用此功能

$configurator = new LoggerConfiguratorDefault();
$logConfig    = $configurator->parse(__DIR__ . "/config/log4php_backend.xml"); // this is where my xml file is located
Logger::configure($logConfig);

这是我编写到应用中的版本

"apache/log4php"          : "^2.3"