Last active
December 29, 2015 14:49
-
-
Save hjue/7686962 to your computer and use it in GitHub Desktop.
Log Library for Codeigniter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php defined('BASEPATH') OR exit('No direct script access allowed'); | |
| /** | |
| * CodeIgniter Log Library | |
| * | |
| * @category Applications | |
| * @package CodeIgniter | |
| * @subpackage Libraries | |
| * @author hjue | |
| * @license BSD License | |
| * @link http://hjue.me | |
| * @since Version 1.0 | |
| */ | |
| /* | |
| drop table logs; | |
| CREATE TABLE IF NOT EXISTS `logs` ( | |
| `id` int(11) NOT NULL AUTO_INCREMENT, | |
| `err_no` int(2) NOT NULL, | |
| `err_type` varchar(32) CHARACTER SET utf8 NOT NULL, | |
| `err_message` text CHARACTER SET utf8 NOT NULL, | |
| `err_file` varchar(255) CHARACTER SET utf8 NOT NULL, | |
| `err_line` int(4) NOT NULL, | |
| `time` datetime NOT NULL, | |
| PRIMARY KEY (`id`) | |
| ) DEFAULT CHARSET=utf8; | |
| */ | |
| class Log_lib { | |
| private $_ci; | |
| private $_sentry_client; | |
| private $_log_table_name; | |
| public $log_levels = array( | |
| E_ERROR => 'Error', | |
| E_WARNING => 'Warning', | |
| E_PARSE => 'Parsing Error', | |
| E_NOTICE => 'Notice', | |
| E_CORE_ERROR => 'Core Error', | |
| E_CORE_WARNING => 'Core Warning', | |
| E_COMPILE_ERROR => 'Compile Error', | |
| E_COMPILE_WARNING => 'Compile Warning', | |
| E_USER_ERROR => 'User Error', | |
| E_USER_WARNING => 'User Warning', | |
| E_USER_NOTICE => 'User Notice', | |
| E_STRICT => 'Runtime Notice', | |
| E_RECOVERABLE_ERROR => 'Catchable error', | |
| E_DEPRECATED => 'Runtime Notice', | |
| E_USER_DEPRECATED => 'User Warning' | |
| ); | |
| public function __construct() | |
| { | |
| $this->_ci =& get_instance(); | |
| set_error_handler(array($this, 'log_error')); | |
| set_exception_handler(array($this, 'log_exception')); | |
| register_shutdown_function(array($this,'shutdown_function')); | |
| if ($this->_ci->config->item('default_log') == 'sentry' and $this->_ci->config->item('raven_dsn')) | |
| { | |
| include APPPATH.'third_party/Raven/Autoloader.php'; | |
| Raven_Autoloader::register(); | |
| $this->_sentry_client = new Raven_Client($this->_ci->config->item('raven_dsn')); | |
| $this->set_user_data(); | |
| } | |
| if($this->_ci->config->item('default_log') == 'database' and $this->_ci->config->item('log_table_name')) | |
| { | |
| $this->_ci->load->database(); | |
| $this->_log_table_name = $this->_ci->config->item('log_table_name'); | |
| } | |
| } | |
| function shutdown_function() { | |
| if (($error = error_get_last()) and $error['type']==E_ERROR) { | |
| $this->log_exception($error['type'],$error['message'],$error['file'],$error['line']); | |
| } | |
| } | |
| private function set_user_data() | |
| { | |
| $this->_ci->load->library('user_lib'); | |
| $username = $this->_ci->user_lib->getCurrUserName(); | |
| if(!empty($username)){ | |
| $this->_sentry_client->set_user_data($username); | |
| } | |
| } | |
| function log_exception($exception) | |
| { | |
| $error_type = $exception->getCode(); | |
| // TODO catch Exception->getCode==0 ,why | |
| // if ($error_type==0) $error_type = 1; | |
| $this->log_error($error_type,$exception->getMessage(),$exception->getFile(),$exception->getLine()); | |
| } | |
| function log_error($severity, $message, $filepath, $line) | |
| { | |
| if (isset($this->log_levels[$severity])) | |
| { | |
| if ($this->_sentry_client) | |
| { | |
| $level = $this->_sentry_client->translateSeverity($severity); | |
| $referer = empty($_SERVER['HTTP_REFERER'])?'':$_SERVER['HTTP_REFERER']; | |
| $options = array( | |
| 'level'=>$level, | |
| 'tags'=>array('referer'=>$referer), | |
| ); | |
| $this->_sentry_client->captureException(new Exception($message),$options); | |
| }else if($this->_log_table_name) | |
| { | |
| $data = array( | |
| 'err_no' => $severity, | |
| 'err_type' => isset($this->log_levels[$severity]) ? $this->log_levels[$severity] : $severity, | |
| 'err_message' => $message, | |
| 'err_file' => $filepath, | |
| 'err_line' => $line, | |
| 'time' => date('Y-m-d H:i:s') | |
| ); | |
| $this->_ci->db->insert($this->_log_table_name, $data); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment