2

I'm configuring log4php with xml. Here my configuration:

<appender name="mainAppender" class="LoggerAppenderDailyFile">
    <layout class="LoggerLayoutPattern">
        <param name="conversionPattern" value="%d{Y-m-d H:i:s.u T P} [%logger] [%level],[%method,%location] %message;%ex%newline" />
    </layout>

    <param name="file" value=".././logs/main_log-%s.log" />
    <param name="datePattern" value="Y-m-d" />

</appender>

<logger name="MainLogger">
    <appender_ref ref="mainAppender" />
</logger>

<root>
    <level value="TRACE" />
    <appender_ref ref="mainAppender" />
</root>

I want the log to be stored always in the same directory, but the above configuration will create the logFile in different place if I call the

Logger::getLogger('MyLogger');

in different location. How can I get the parent directory inside the XML? I was thinking about something like:

<param name="file" value=dirname('/').".././logs/main_log-%s.log" />

That of course is not working. Normally in php to avoid this problem I'm using:

include_once dirname('/').'.././libs/log4php/Logger.php' ;

Thanks for your help

Stefano Giacone
  • 2,016
  • 3
  • 27
  • 50

1 Answers1

1

You can (and probably should) use a PHP-array based configuration for Log4PHP. This allows you to use dynamic code to create the configuration.

And by the way: There is the magic constant __DIR__ which contains the directory the current PHP file is in. At the same time __FILE__ contains the filename of the current PHP file. That dirname('/') trick shouldn't work - it will output the string "/" - combining it with a path starting with ".." will result in the attempt to go one level beyond the root directory - which is the root directory.

Accessing the path "." is also useless, because the dot is a synonym for "the current directory". A dot in the middle of the path will go nowhere, it will stay where it is.

So dirname('/').'.././libs/log4php/Logger.php' is equivalent to "/.././libs/log4php/Logger.php", which is equivalent to "/libs/log4php/Logger.php". If by accident your file was there, you can misinterpret your code as "working".

Sven
  • 69,403
  • 10
  • 107
  • 109