Created
October 22, 2012 09:58
-
-
Save 755/3930723 to your computer and use it in GitHub Desktop.
Change codeigniter Log class to work with Raven-php
This file contains 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 | |
class MY_Log extends CI_Log { | |
protected $_threshold = 1; | |
protected $_enabled = TRUE; | |
protected $_levels = array('ERROR' => '1', 'DEBUG' => '2', 'INFO' => '3', 'ALL' => '4'); | |
protected $_raven_levels = array(); | |
protected $_client; | |
/** | |
* Constructor | |
*/ | |
public function __construct() | |
{ | |
$config =& get_config(); | |
require_once APPPATH.'libraries/raven-php/lib/Raven/Autoloader.php'; | |
Raven_Autoloader::register(); | |
//Translates CI log levels to Raven log levels. | |
$this->_raven_levels = array('ERROR' => Raven_Client::ERROR, 'DEBUG' => Raven_Client::DEBUG, 'INFO' => Raven_Client::INFO, 'ALL' => Raven_Client::DEBUG); | |
// Instantiate a new client with a compatible DSN | |
$this->_client = new Raven_Client($config['public_DSN']); | |
$error_handler = new Raven_ErrorHandler($this->_client); | |
$error_handler->registerExceptionHandler(); | |
if (is_numeric($config['log_threshold'])) | |
{ | |
$this->_threshold = $config['log_threshold']; | |
} | |
} | |
// -------------------------------------------------------------------- | |
/** | |
* Write Log File | |
* | |
* Generally this function will be called using the global log_message() function | |
* | |
* @param string the error level | |
* @param string the error message | |
* @param bool whether the error is a native PHP error | |
* @return bool | |
*/ | |
public function write_log($level = 'error', $msg, $php_error = FALSE) | |
{ | |
if ($this->_enabled === FALSE) | |
{ | |
return FALSE; | |
} | |
$level = strtoupper($level); | |
if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold)) | |
{ | |
return FALSE; | |
} | |
$this->_client->captureMessage($msg,array(),$this->_raven_levels[$level],TRUE); | |
return TRUE; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment