[
https://issues.apache.org/jira/browse/LOG4PHP-114?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13062594#comment-13062594
]
Olivier ROMAND commented on LOG4PHP-114:
----------------------------------------
Hey guys,
First thanks for all the work you achieve with log4php. We are using it for a
very big commercial project and log4php is quite a nice tool to have.
We also noticed the bad behavior described above, especially in .ini file.
Consider the example:
<?xml version="1.0" encoding="UTF-8"?>
<log4php:configuration xmlns:log4php="http://logging.apache.org/log4php/">
<appender name="A1" class="LoggerAppenderFile">
<param name="file" value="/var/log/a1.log" />
</appender>
<appender name="A2" class="LoggerAppenderFile">
<param name="file" value="/var/log/a2.log" />
</appender>
<appender name="A3" class="LoggerAppenderFile">
<param name="file" value="/var/log/a3.log" />
</appender>
<appender name="A4" class="LoggerAppenderFile">
<param name="file" value="/var/log/a4.log" />
</appender>
<root>
<appender_ref ref="A1" />
</root>
<logger name="foo" additivity="false">
<appender_ref ref="A2" />
<appender_ref ref="A3" />
</logger>
<logger name="foo.bar" />
<logger name="foo.bar.baz" additivity="false">
<appender_ref ref="A4" />
</logger>
</log4php:configuration>
The traduction in .ini file should be something like :
log4php.appender.A1 = LoggerAppenderFile
log4php.appender.A1.file = /var/log/filea1.log
log4php.appender.A2 = LoggerAppenderFile
log4php.appender.A2.file = /var/log/filea2.log
log4php.appender.A3 = LoggerAppenderFile
log4php.appender.A3.file = /var/log/filea3.log
log4php.appender.A4 = LoggerAppenderFile
log4php.appender.A4.file = /var/log/filea4.log
log4php.rootLogger = DEBUG, A1
log4php.logger.foo = DEBUG, A2, A3
log4php.logger.foo.bar = DEBUG, A2, A3
log4php.logger.foo.bar.baz = DEBUG, A4
log4php.additivity.logger.foo.bar.baz = "false"
Problem: this configuration would explode.
I took the time to debug the parsing of the file and the construction of the
objects and discovered the use of the function each(), which has some really
nice side effect.
The documentation clearly says: After each() has executed, the array cursor
will be left on the next element of the array, or past the last element if it
hits the end of the array. You have to use reset() if you want to traverse the
array again using each.
link: http://www.php.net/manual/en/function.each.php
So what happens is the property file is parsed through but never reset. The
first search would set the file to the correct value, but the research of the
file parameters for the second appender (consider A2) would call setFile(null),
as the pointer at this moment is located somewhere line 'log4php.rootLogger =
DEBUG, A1'
I don't know if the reason is some really nice coding style feature or some
effect to increase performances but it looks like some side effect bug.
Using each() appears to be very dangerous for anybody taking the project "as
is". The parsing of the file is dependant to the order of some parameters and
this should be avoid.
A quick fix is to call reset before using the function each() (appear to be
present only twice), to make sure the pointer would be set back to the
beginning, a nicer solution would be the use of the function foreach that would
avoid this side effect.
The quick fix I added in my code (v. 2.0):
//taken from LoggerConfigurationIni.php, line 335
private function parseCatsAndRenderers($props, LoggerHierarchy $hierarchy) {
reset($props);
while(list($key,$value) = each($props)) ...
//taken from LoggerReflectionUtils.php, line 70
public function setProperties($properties, $prefix) {
$len = strlen($prefix);
reset($properties);
while(list($key,) = each($properties)) ...
Finally, the additivity of the appender won't work with the value
-log4php.additivity.logger.foo.bar.baz = false
but
-log4php.additivity.logger.foo.bar.baz = "false"
would work.
Looks like this is also some feature to have, or explain it more clearly in the
documentation.
I would be glad to hear from your thougths.
Olivier
> Order of params in configuration is significant
> -----------------------------------------------
>
> Key: LOG4PHP-114
> URL: https://issues.apache.org/jira/browse/LOG4PHP-114
> Project: Log4php
> Issue Type: Bug
> Components: Code
> Affects Versions: 2.0
> Reporter: Darja Ryazhskikh
> Fix For: 2.2
>
>
> For example. Config as:
> <appender name="default" class="LoggerAppenderDailyFile">
> <param name="datePattern" value="Y-m-d" />
> <param name="file" value="logs/%s.log" />
> </appender>
> works not as:
> <appender name="default" class="LoggerAppenderDailyFile">
> <param name="file" value="logs/%s.log" />
> <param name="datePattern" value="Y-m-d" />
> </appender>
> That's because of setFile happens before datePattern is set. But it is
> unexpected behavior for user.
--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira