CodeIgniter Forums
Using Custom Log Helper - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Using Custom Log Helper (/showthread.php?tid=77321)



Using Custom Log Helper - jjohn379 - 08-16-2020

Hello I am trying to implement a Database Log Handler to store logs directly to the database, but I am having trouble adding the new logger. My class is not being found when I log a new message. I continuously get this error:

   
 Any help would be appreciated! I have added the new logger to the Config\Logger.php file like this: 


PHP Code:
'App\Libraries\DatabaseLogHandler'  => [
                
/*
                 * The log levels that this handler will handle.
                 */
                
'handles' => ['critical''alert''emergency''debug',
                            
  'error''info''notice''warning'],
            ] 



And I have the DatabaseLogHandler.php file in my Libraries folder (should I put this in a different folder?):
PHP Code:
<?php

namespace App\Libraries;

use 
App\Entities\Log;
use 
App\Models\LogModel;
use 
CodeIgniter\Log\Handlers\BaseHandler;
use 
CodeIgniter\Log\Handlers\HandlerInterface;

class 
DatabaseLogHandler extends BaseHandler implements HandlerInterface
{
    protected $logModel;
    protected $log;

    public function __construct(array $config = [])
    {
        parent::__construct($config);

        $this->logModel = new LogModel();
        $this->log = new Log();

    }

    public function handle($level$message): bool
    

        $this->log->log_message $message;
        $this->log->level $level;
        $this->log->status "Open";
        $this->log->log_type "Website";
        $this->log->setCreatedAt(date('Y-m-d H:i:s'));
        $this->logModel->insert($this->log);
        return true;
    }




RE: Using Custom Log Helper - InsiteFX - 08-17-2020

How are you calling it? No it's in the right place so something else is wrong.


RE: Using Custom Log Helper - jjohn379 - 08-17-2020

The issue seems to be resolved now. I was logging the message incorrectly.