[eluser]jonez[/eluser]
My initial solution for logging PHP errors to a database using CI3-dev:
db.error
Code:
CREATE TABLE `error` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`code` varchar(255) NOT NULL,
`message` text NOT NULL,
`file_path` varchar(255) NOT NULL,
`line_number` varchar(255) NOT NULL,
`ip_address` varchar(255) NOT NULL,
`created_datetime` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=latin1;
/application/config/hooks.php
Code:
$hook['post_controller_constructor'][] = array(
'function' => 'check_db',
'filename' => 'log.php',
'filepath' => 'hooks',
);
/application/hooks/log.php
Code:
<?php
function check_db( ) {
$ci =& get_instance( );
$ci->log->to_database( true );
}
?>
/application/core/MY_Log.php
Code:
<?php if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
class MY_Log extends CI_Log {
protected $_log_db;
public function to_database( $status = false ) {
$this->_log_db = $status;
}
public function write_log( $level, $msg ) {
parent::write_log( $level, $msg );
$level = strtoupper( $level );
if ( ( !isset( $this->_levels[ $level ] ) || ( $this->_levels[ $level ] > $this->_threshold ) ) && !isset( $this->_threshold_array[ $this->_levels[ $level ] ] ) ) {
return false;
}
if ( $this->_log_db == true ) {
$ci =& get_instance( );
$ci->load->database( );
$s = array( 'Severity: ', 'Notice -->', 'Warning -->', 'Error -->' );
$r = array( '', '', '', '' );
$msg = trim( str_replace( $s, $r, $msg ) );
$line = trim( substr( $msg, strrpos( $msg, ' ' ) ) );
$re = preg_match( '/\/[^ ]+/', $msg, $path );
$path = $path[ 0 ];
$msg = trim( substr( $msg, 0, strpos( $msg, '/' ) ) );
$data = array(
'code' => 500,
'message' => $msg,
'file_path' => $path,
'line_number' => $line,
'ip_address' => $_SERVER[ 'REMOTE_ADDR' ],
'created_datetime' => date( 'Y-m-d H:i:s' ),
);
$ci->db->insert( 'error', $data );
}
}
}
?>