log4php

dynamic file path in log4php


I am new to log4php.

I would like to save the log files in the format /logs/UserId/Info_ddmmyyyy.php where the UserId is dynamic data.

(I would basically like to save one log per user.)

Is there any way to change the log file path dynamically?


Solution

  • This behaviour is not supported by default. But you can extend LoggerAppenderFile (or RollingFile, DailyFile whatever your preference is) to support it.

    Create your own class for that and make it load to your script.

    Then extend from this class: http://svn.apache.org/repos/asf/logging/log4php/trunk/src/main/php/appenders/LoggerAppenderFile.php

    class MyAppender extends LoggerAppenderFile { ... }
    

    You'll need to overwrite the setFile() method, similar to:

    public function setFile($file) {
        $path = getYourFullPath();
        $this->file = $path.$file;
    }
    

    After all you need to use your new Appender in you config

    log4php.appender.myAppender = MyAppender
    log4php.appender.myAppender.layout = LoggerLayoutSimple
    log4php.appender.myAppender.file = my.log
    

    Please note, instead of giving your full path to the log file you now need to add a plain name. The full path (including username) must be calculated with your getYourFullPath() method.

    Hope that helps! Christian