Welcome Guest, Not a member yet? Register   Sign In
Storing Error Logs in Database
#1

I am trying to find the easiest path to storing log events in a database as opposed to in a log file. It seems like the only way this can be done is by extending the logger class here vendor\codeigniter4\framework\system\Log\Logger.php and then creating an additional method that is invoked when the "write to file" method is invoked, changing it with a "write to database" method. 

I will need to extend the core functionality(creating an additional class), per the documentation: https://www.codeigniter.com/user_guide/e...re-classes

Any recommendations or code sharing is greatly appreciated.
Reply
#2

(This post was last modified: 10-02-2023, 11:58 AM by mikestratton.)

I believe I found the answer. CodeIgniter\Log\Handlers\FileHandler is a core class, therefore, the method in FileHandler: public function handle() can be edited to store information in the database as opposed to a file. This can be done by extending a core class: https://www.codeigniter.com/user_guide/e...re-classes
Reply
#3

Create your database handler based on FileHandler, and specify it.
https://codeigniter4.github.io/CodeIgnit...g-handlers
Reply
#4

(This post was last modified: 10-04-2023, 09:23 AM by mikestratton.)

In the app/Config/Logger.php file, I added:
public array $handlers = [
        /*
        * --------------------------------------------------------------------
        * Custom Database Log Handler
        * --------------------------------------------------------------------
        */
        // 'CodeIgniter\Log\Handlers\DatabaseLogHandler' => [
        DatabaseLogHandler::class => [
            /*
            * The log levels that this handler will handle.
            */
            'handles' => ['critical', 'alert', 'emergency', 'debug',
                          'error', 'info', 'notice', 'warning'],
        ],

Note that it points to: vendor\codeigniter4\framework\system\Log\Handlers\DatabaseLogHandler.php

In DatabaseLogHandler.php:
public function handle($level, $message): bool
    {
        $db      = \Config\Database::connect();
        $builder = $db->table('your database name here');

        $msg = '';

        // Instantiating DateTime with microseconds appended to initial date is needed for proper support of this format
        if (strpos($this->dateFormat, 'u') !== false) {
            $microtimeFull  = microtime(true);
            $microtimeShort = sprintf('%06d', ($microtimeFull - floor($microtimeFull)) * 1_000_000);
            $date          = new DateTime(date('Y-m-d H:i:s.' . $microtimeShort, (int) $microtimeFull));
            $date          = $date->format($this->dateFormat);
        } else {
            $date = date($this->dateFormat);
        }

        $level = strtoupper($level);
        $msg .=  $date . ' --> ' . $message . "\n";

        $data = [
            'LOG_TYPE'          => $level,
            'LOG_MESSAGE_STRING' => $msg
        ];
       
       
        $result = false;
        $builder->insert($data);

        if($builder->insert($data) == true){
            $result = true;
        }

        return $result;
    }
Reply




Theme © iAndrew 2016 - Forum software by © MyBB