PHP code example of brunoggdev / ci4-logging-extended

1. Go to this page and download the library: Download brunoggdev/ci4-logging-extended library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

brunoggdev / ci4-logging-extended example snippets


public static function logger(bool $getShared = true)
{
    if ($getShared) {
        return static::getSharedInstance('logger');
    }

    return new \Brunoggdev\LoggingExtended\Logger(config('Logger'));
}

// Native CI4 (PSR-3 strict) — context is discarded, you only see "copyProducts failed"
log_message('error', 'copyProducts failed', [
    'source' => $sourceId,
    'target' => $targetId,
    'error'  => $e->getMessage(),
]);

logger()->error('Job {job} failed on attempt {attempt}', [
    'job'     => 'SendEmail',   // consumed by {job}
    'attempt' => 3,             // consumed by {attempt}
    'user_id' => 99,            // not a placeholder — appended
]);
// ERROR - ... --> Job SendEmail failed on attempt 3 | user_id=99

try {
    // ...
} catch (Throwable $e) {
    logger()->exception($e);            // defaults to 'error' level
    logger()->exception($e, 'warning'); // or any PSR-3 level
}
// ERROR - ... --> [RuntimeException] Something went wrong

use Brunoggdev\LoggingExtended\Logger;

class GlitchTipLogger extends Logger
{
    public function exception(Throwable $e, string $level = 'error'): void
    {
        \Sentry\captureException($e);
        parent::exception($e, $level);
    }
}

// Before
use CodeIgniter\Log\Logger;

// After
use Brunoggdev\LoggingExtended\Logger;
bash
php spark log:tail

  ERROR      2026-03-20 14:32:01  copyUnityProducts failed | source_unity_id=42 error="Timeout"
  WARNING    2026-03-20 14:32:05  Retry scheduled | job=CopyProducts attempt=2
  INFO       2026-03-20 14:32:10  Job completed successfully