separate output for different log levels in log4php
im kinda new with log4php.. i need to output the INFO and DEBUG levels in different files. is that possible? my xml looks like this:
<appender name="dlog" class="LoggerAppenderRollingFile">
<param name="file开发者_JS百科" value="C:/log/dlog"></param></appender>
<appender name="ilog" class="LoggerAppenderRollingFile">
<param name="file" value="C:/log/ilog/"></param></appender>
<root>
<level value="DEBUG" />
<appender_ref ref="dlog" ></appender_ref>
</root>
<logger name="myLogger">
<level value="INFO"/>
<appender-ref ref="ilog" />
</logger>
**This outputs only the INFO levels in the corresponding filename please help :( thanks a lot.
This can be achieved using multiple appenders with filters and one logger.
Overview
You can define multiple appenders to send the logging to different destinations. We shall define 1 appender for each level and each appender will be logging to different files.
In addition to these, we need to create a filter for each appender. We will use the filter LoggerFilterLevelRange and we shall specify a min and max level. Note the filter LoggerFilterLevelMatch cannot be used as it matches a filter but is neutral to the other filters.
As for the loggers, we shall attach all our appenders to the one and only logger.
This way, you can send the different logging messages to different files depending on the error level.
Configuration
<appender name="dlog" class="LoggerAppenderRollingFile">
<param name="file" value="C:/log/dlog" />
<filter class="LoggerFilterLevelRange">
<param name="levelMin" value="debug" />
<param name="levelMax" value="debug" />
</filter>
</appender>
<appender name="ilog" class="LoggerAppenderRollingFile">
<param name="file" value="C:/log/ilog" />
<filter class="LoggerFilterLevelRange">
<param name="levelMin" value="info" />
<param name="levelMax" value="info" />
</filter>
</appender>
<root>
<level value="INFO" />
<appender_ref ref="ilog" />
<appender_ref ref="dlog" />
</root>
</configuration>
Usage
require_once 'Logger.php';
Logger::configure('log_config.xml');
$logger = Logger::getLogger();
$logger->info('This message is meant to inform.');
$logger->debug('A debug message!');
I hope this helps.
I think the appropriate solution would be to apply a filter and a threshold to your appenders. Like so:
<appender name="dlog" class="LoggerAppenderRollingFile">
<param name="file" value="C:/log/dlog" />
<filter class="LoggerFilterLevelMatch">
<param name="LevelToMatch" value="debug" />
<param name="AcceptOnMatch" value="true" />
</filter>
<filter class="LoggerFilterDenyAll" />
</appender>
<appender threshold="INFO" name="ilog" class="LoggerAppenderRollingFile">
<param name="file" value="C:/log/ilog/" />
</appender>
<root>
<level value="DEBUG" />
<appender_ref ref="dlog" />
<appender_ref ref="ilog" />
</root>
This would only allow messages with debug
level into dlog
and (due to the threshold) only info
and above into ilog
. If you didn't want messages above info
level in ilog
, then you could apply the same filter as with the dlog
appender.
精彩评论