Changeset View
Changeset View
Standalone View
Standalone View
branches/5.2.x/core/kernel/utility/logger.php
Show First 20 Lines • Show All 144 Lines • ▼ Show 20 Line(s) | |||||
const LNS_PENDING = 1; | const LNS_PENDING = 1; | ||||
/** | /** | ||||
* Log notification status: sent | * Log notification status: sent | ||||
*/ | */ | ||||
const LNS_SENT = 2; | const LNS_SENT = 2; | ||||
/** | /** | ||||
* Database connection used for logging. | |||||
* | |||||
* @var kDBConnection | |||||
*/ | |||||
protected $dbStorage; | |||||
/** | |||||
* List of error/exception handlers | * List of error/exception handlers | ||||
* | * | ||||
* @var Array | * @var Array | ||||
* @access protected | * @access protected | ||||
*/ | */ | ||||
protected $_handlers = Array (); | protected $_handlers = Array (); | ||||
/** | /** | ||||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Line(s) | |||||
* | * | ||||
* @param Array $methods_to_call List of invokable kLogger class method with their parameters (if any) | * @param Array $methods_to_call List of invokable kLogger class method with their parameters (if any) | ||||
* @access public | * @access public | ||||
*/ | */ | ||||
public function __construct($methods_to_call = Array ()) | public function __construct($methods_to_call = Array ()) | ||||
{ | { | ||||
parent::__construct(); | parent::__construct(); | ||||
$this->dbStorage = $this->getDBStorage(); | |||||
$system_config = kUtil::getSystemConfig(); | $system_config = kUtil::getSystemConfig(); | ||||
$this->_debugMode = $this->Application->isDebugMode(); | $this->_debugMode = $this->Application->isDebugMode(); | ||||
$this->setState($system_config->get('EnableSystemLog', self::STATE_DISABLED)); | $this->setState($system_config->get('EnableSystemLog', self::STATE_DISABLED)); | ||||
$this->_maxLogLevel = $system_config->get('SystemLogMaxLevel', self::LL_NOTICE); | $this->_maxLogLevel = $system_config->get('SystemLogMaxLevel', self::LL_NOTICE); | ||||
foreach ($methods_to_call as $method_to_call) { | foreach ($methods_to_call as $method_to_call) { | ||||
call_user_func_array(Array ($this, $method_to_call[0]), $method_to_call[1]); | call_user_func_array(Array ($this, $method_to_call[0]), $method_to_call[1]); | ||||
} | } | ||||
if ( !kUtil::constOn('DBG_ZEND_PRESENT') && !$this->Application->isDebugMode() ) { | if ( !kUtil::constOn('DBG_ZEND_PRESENT') && !$this->Application->isDebugMode() ) { | ||||
// don't report error on screen if debug mode is turned off | // don't report error on screen if debug mode is turned off | ||||
error_reporting(0); | error_reporting(0); | ||||
ini_set('display_errors', 0); | ini_set('display_errors', 0); | ||||
} | } | ||||
register_shutdown_function(Array ($this, 'catchLastError')); | register_shutdown_function(Array ($this, 'catchLastError')); | ||||
} | } | ||||
/** | /** | ||||
* Create separate connection for logging purposes. | |||||
* | |||||
* @return kDBConnection | |||||
*/ | |||||
protected function getDBStorage() | |||||
{ | |||||
$system_config = new kSystemConfig(true); | |||||
$vars = $system_config->getData(); | |||||
$db_class = $this->Application->isDebugMode() ? 'kDBConnectionDebug' : 'kDBConnection'; | |||||
// Can't use "kApplication::makeClass", because class factory isn't initialized at this point. | |||||
$db = new $db_class(SQL_TYPE, array($this, 'handleSQLError'), 'logger'); | |||||
$db->setup($vars); | |||||
return $db; | |||||
} | |||||
/** | |||||
* Sets state of the logged (enabled/user-only/disabled) | * Sets state of the logged (enabled/user-only/disabled) | ||||
* | * | ||||
* @param $new_state | * @param $new_state | ||||
* @return void | * @return void | ||||
* @access public | * @access public | ||||
*/ | */ | ||||
public function setState($new_state = null) | public function setState($new_state = null) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 511 Lines • ▼ Show 20 Line(s) | |||||
return false; | return false; | ||||
} | } | ||||
$this->_logRecord['LogMemoryUsed'] = memory_get_usage(); | $this->_logRecord['LogMemoryUsed'] = memory_get_usage(); | ||||
$this->_logRecord['LogTimestamp'] = adodb_mktime(); | $this->_logRecord['LogTimestamp'] = adodb_mktime(); | ||||
$this->_logRecord['LogDate'] = adodb_date('Y-m-d H:i:s'); | $this->_logRecord['LogDate'] = adodb_date('Y-m-d H:i:s'); | ||||
if ( $storage_medium == self::LS_AUTOMATIC ) { | if ( $storage_medium == self::LS_AUTOMATIC ) { | ||||
$storage_medium = $this->Conn->connectionOpened() ? self::LS_DATABASE : self::LS_DISK; | $storage_medium = $this->dbStorage->connectionOpened() ? self::LS_DATABASE : self::LS_DISK; | ||||
} | } | ||||
if ( $storage_medium == self::LS_DATABASE ) { | if ( $storage_medium == self::LS_DATABASE ) { | ||||
$result = $this->Conn->doInsert($this->_logRecord, TABLE_PREFIX . 'SystemLog'); | $result = $this->dbStorage->doInsert($this->_logRecord, TABLE_PREFIX . 'SystemLog'); | ||||
} | } | ||||
elseif ( $storage_medium == self::LS_DISK ) { | elseif ( $storage_medium == self::LS_DISK ) { | ||||
$result = $this->_saveToFile(RESTRICTED . '/system.log'); | $result = $this->_saveToFile(RESTRICTED . '/system.log'); | ||||
} | } | ||||
else { | else { | ||||
throw new InvalidArgumentException('Unknown storage medium "' . $storage_medium . '"'); | throw new InvalidArgumentException('Unknown storage medium "' . $storage_medium . '"'); | ||||
} | } | ||||
Show All 34 Lines | |||||
* @param int $storage_medium | * @param int $storage_medium | ||||
* @return void | * @return void | ||||
* @access public | * @access public | ||||
* @throws InvalidArgumentException | * @throws InvalidArgumentException | ||||
*/ | */ | ||||
public function delete($unique_id, $storage_medium = self::LS_AUTOMATIC) | public function delete($unique_id, $storage_medium = self::LS_AUTOMATIC) | ||||
{ | { | ||||
if ( $storage_medium == self::LS_AUTOMATIC ) { | if ( $storage_medium == self::LS_AUTOMATIC ) { | ||||
$storage_medium = $this->Conn->connectionOpened() ? self::LS_DATABASE : self::LS_DISK; | $storage_medium = $this->dbStorage->connectionOpened() ? self::LS_DATABASE : self::LS_DISK; | ||||
} | } | ||||
if ( $storage_medium == self::LS_DATABASE ) { | if ( $storage_medium == self::LS_DATABASE ) { | ||||
$sql = 'DELETE FROM ' . TABLE_PREFIX . 'SystemLog | $sql = 'DELETE FROM ' . TABLE_PREFIX . 'SystemLog | ||||
WHERE LogUniqueId = ' . $unique_id; | WHERE LogUniqueId = ' . $unique_id; | ||||
$this->Conn->Query($sql); | $this->dbStorage->Query($sql); | ||||
} | } | ||||
elseif ( $storage_medium == self::LS_DISK ) { | elseif ( $storage_medium == self::LS_DISK ) { | ||||
// TODO: no way to delete a line from a file | // TODO: no way to delete a line from a file | ||||
} | } | ||||
else { | else { | ||||
throw new InvalidArgumentException('Unknown storage medium "' . $storage_medium . '"'); | throw new InvalidArgumentException('Unknown storage medium "' . $storage_medium . '"'); | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 643 Lines • Show Last 20 Lines |