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.



32.4k

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

<?php
error_reporting(E_ALL);

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');
     $logger->error($e->getMessage());
}

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



2.3k

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..



2.3k

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.phalconphp.com/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");
}

http://docs.phalconphp.com/en/latest/reference/logging.html#file-logger



2.3k

That worked, Nikolaos.

Thanks, Zane