Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kenjis/aa2700f76c456f06f58134271a8ce2be to your computer and use it in GitHub Desktop.
Save kenjis/aa2700f76c456f06f58134271a8ce2be to your computer and use it in GitHub Desktop.
How to Log URI or etc. when Exception occurs in CodeIgniter 4

How to Log URI or etc. when Exception occurs in CodeIgniter 4

Create your custom Exception Handler

<?php

namespace App\Libraries;

use CodeIgniter\Debug\BaseExceptionHandler;
use CodeIgniter\Debug\ExceptionHandler;
use CodeIgniter\Debug\ExceptionHandlerInterface;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Throwable;

class MyExceptionHandler extends BaseExceptionHandler implements ExceptionHandlerInterface
{
    // You can override the view path.
    protected ?string $viewPath = APPPATH . 'Views/exception/';

    public function handle(
        Throwable $exception,
        RequestInterface $request,
        ResponseInterface $response,
        int $statusCode,
        int $exitCode
    ): void {
        if (! in_array($statusCode, $this->config->ignoreCodes, true)) {
            log_message(
                'critical',
                "{message}\nin {exFile} on line {exLine}.",
                [
                    'message' => '["' . $request->getPath() . '","'
                        . $request->getIPAddress() . '","'
                        . $request->getUserAgent() . '"] '
                        . $exception->getMessage(),
                    'exFile' => clean_path($exception->getFile()),
                    // {file} refers to THIS file
                    'exLine' => $exception->getLine(),
                    // {line} refers to THIS line
                ]
            );
        }

        $frameworkHandler = new ExceptionHandler($this->config);
        $frameworkHandler->handle(
            $exception,
            $request,
            $response,
            $statusCode,
            $exitCode
        );
    }
}

Configure to use it

--- a/app/Config/Exceptions.php
+++ b/app/Config/Exceptions.php
@@ -2,8 +2,8 @@
 
 namespace Config;
 
+use App\Libraries\MyExceptionHandler;
 use CodeIgniter\Config\BaseConfig;
-use CodeIgniter\Debug\ExceptionHandler;
 use CodeIgniter\Debug\ExceptionHandlerInterface;
 use Psr\Log\LogLevel;
 use Throwable;
@@ -99,6 +99,6 @@ class Exceptions extends BaseConfig
      */
     public function handler(int $statusCode, Throwable $exception): ExceptionHandlerInterface
     {
-        return new ExceptionHandler($this);
+        return new MyExceptionHandler($this);
     }
 }

Log sample

You can see the log like this:

CRITICAL - 2023-10-27 08:50:28 --> syntax error, unexpected token "}", expecting ";"
in APPPATH/Controllers/Home.php on line 10.
 1 SYSTEMPATH/Autoloader/Autoloader.php(291): CodeIgniter\Autoloader\Autoloader->includeFile('/.../CodeIgniter4/app/Controllers/Home.php')
 2 SYSTEMPATH/Autoloader/Autoloader.php(269): CodeIgniter\Autoloader\Autoloader->loadInNamespace('App\\Controllers\\Home')
 3 [internal function]: CodeIgniter\Autoloader\Autoloader->loadClass('App\\Controllers\\Home')
 4 SYSTEMPATH/CodeIgniter.php(900): class_exists('\\App\\Controllers\\Home', true)
 5 SYSTEMPATH/CodeIgniter.php(489): CodeIgniter\CodeIgniter->startController()
 6 SYSTEMPATH/CodeIgniter.php(361): CodeIgniter\CodeIgniter->handleRequest(null, Object(Config\Cache), false)
 7 FCPATH/index.php(79): CodeIgniter\CodeIgniter->run()
 8 SYSTEMPATH/Commands/Server/rewrite.php(47): require_once('/.../CodeIgniter4/public/index.php')
CRITICAL - 2023-10-27 08:50:28 --> ["/home","192.168.1.12","Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/118.0"] syntax error, unexpected token "}", expecting ";"
in APPPATH/Controllers/Home.php on line 10.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment