Welcome Guest, Not a member yet? Register   Sign In
Codeigniter 4 custom log levels
#2

After some more tinkering, I found a way to override handler config for specified logging level to allow custom file extension and save path.
Extended logger->log() method with AufitLogger->customLog() method as follows. This may be hacky, but it appears to work.


app/Log/AuditLogger
PHP Code:
public function customLog($levelstring|Stringable $message, array $context = []): void
    
{
        if ($level === 'audit') {

            // assuming the first or the only handler is the correct one (FileHandler in our case)
            $handlerClassName array_keys($this->handlerConfig)[0];
            $savePath WRITEPATH 'custom_logs/';

            $this->handlerConfig[$handlerClassName]['fileExtension'] = 'tmp';
            $this->handlerConfig[$handlerClassName]['path'] = $savePath;

            // re-initialize handler with changed config
            $this->handlers[$handlerClassName] = new $handlerClassName($this->handlerConfig[$handlerClassName]);
            
            
// run parent log() method with updated handler
            parent::log($level$message$context);
        }
    
Reply


Messages In This Thread
Codeigniter 4 custom log levels - by B-and-P - 12-16-2024, 05:13 AM
RE: Codeigniter 4 custom log levels - by B-and-P - 12-16-2024, 07:05 AM



Theme © iAndrew 2016 - Forum software by © MyBB