We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Debug to log file [SOLVED]

Is there a way to enable global logging of debug errors to a log file? For example in a Laravel project every request is logged under app/storage/logs. It looks something like:

0 /var/www/core/app/filters.php(17): Illuminate\Exception\Handler->handleError(8, 'Undefined varia...', '/var/www/core...', 17, Array)

1 [internal function]: {closure}(Object(Illuminate\Http\Request))

Since PHP isnt compiled this is huge when trying to debug why something isn't working. I usually just run a tail on this file and use it anytime I get an error.


I am not sure what you mean. But you can use a try catch with logger:


try {
     * Read the configuration
    $config = include __DIR__ . "/../app/config/config.php";

     * Read auto-loader
    include __DIR__ . "/../app/config/loader.php";

     * Read services
    include __DIR__ . "/../app/config/services.php";

     * Handle the request
    $application = new \Phalcon\Mvc\Application($di);

    echo $application->handle()->getContent();

} catch (\Exception $e) {
     $logger = new \Phalcon\Logger\Adapter\File('/tmp/my-log.log');

Depends on how and what do you want to log. See http://docs.phalcon.io/en/latest/reference/logging.html


Basically, I see that phalcon has a debug function, Phalcon\Debug. Instead of printing the backtrace to screen I would rather have it logged to a file so I can tail -f that file and see where the error is occurring in my code.

Say for example I try to call an undefined method in an array, it should spit an error out to that log saying 'Undefined Index' or something to that sorts..


This is exactly what I am trying to accomplish from the documentation "It’s therefore easy to find which file and line of the application’s code generated the exception," http://docs.phalcon.io/en/latest/reference/debug.html

How would I get this to output to /logs/current-date.log

} catch(\Exception $e) {
    echo get_class($e), ": ", $e->getMessage(), "\n";
    echo " File=", $e->getFile(), "\n";
    echo " Line=", $e->getLine(), "\n";
    echo $e->getTraceAsString();

You can use the logger component or a simple error_log would do the trick

} catch(\Exception $e) {
    $message = get_class($e), ": ", $e->getMessage(), "\n"
             . " File=", $e->getFile(), "\n"
             . " Line=", $e->getLine(), "\n"
             . $e->getTraceAsString() . "\n";
    error_log($message, 3, "/logs/current-date.log");



That worked, Nikolaos.

Thanks, Zane