![]() |
Storing Error Logs in Database - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Storing Error Logs in Database (/showthread.php?tid=88595) |
Storing Error Logs in Database - mikestratton - 10-02-2023 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/extending/core_classes.html#extending-core-classes Any recommendations or code sharing is greatly appreciated. RE: Storing Error Logs in Database - mikestratton - 10-02-2023 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/extending/core_classes.html#extending-core-classes RE: Storing Error Logs in Database - kenjis - 10-02-2023 Create your database handler based on FileHandler, and specify it. https://codeigniter4.github.io/CodeIgniter4/general/logging.html#using-multiple-log-handlers RE: Storing Error Logs in Database - mikestratton - 10-03-2023 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; } |