We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to define the logger class dir

Hi,

I want to use the standard logger class with invo app. The example in the documentation is:

$logger = new \Phalcon\Logger\Adapter\File("app/logs/test.log");

but this is not working, nor is the /../app/logs/test.log path. Logger is working only when I give it the full path to the file.

My question is: is there a way to give to logger a short path?

Thanks

I believe (but might be wrong) that the logger is invoked in many places in an application. Those places can be different folders and an absolute path would take into account of where the file that invokes the logger is.

I believe that there is no way for the logger to accept a short path. In my applications I always use a path that is defined with a ROOT_PATH constant which is defined upon startup. ROOT_PATH is where all the folders are /app, /library, /var. /public etc. and the path is set with that.

Example: https://github.com/niden/phalcon-angular-harryhogfootball/blob/master/public/index.php https://github.com/niden/phalcon-angular-harryhogfootball/blob/master/app/library/NDN/Bootstrap.php#L323



7.9k
Accepted
answer
edited Oct '14

Thanks for the quick answer! This is a good solution - the root path could even be included in $config.

$config->application->rootDir = dirname(__DIR__)."/";
/**
* App log
 */
$di->set('logger', function() use ($config) {
        // $config->application->logFile is defined in config.ini as "app/logs/app.log"
    return new \Phalcon\Logger\Adapter\File($config->application->rootDir.$config->application->logFile);
});