Skip to content

Commit

Permalink
Refactored Error Handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rnavarro committed Sep 21, 2011
1 parent b7b6363 commit 06397d5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
25 changes: 15 additions & 10 deletions config/schema/schema.php
@@ -1,5 +1,5 @@
<?php
/* database_logger schema generated on: 2011-08-08 16:39:37 : 1312843177*/
/* database_logger schema generated on: 2011-09-20 18:24:33 : 1316568273*/
class database_loggerSchema extends CakeSchema {
var $name = 'database_logger';

Expand All @@ -12,15 +12,20 @@ function after($event = array()) {

var $logs = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'key' => 'primary'),
'type' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 50, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'message' => array('type' => 'text', 'null' => false, 'default' => NULL, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'ip' => array('type' => 'string', 'null' => true, 'default' => NULL, 'length' => 50, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'hostname' => array('type' => 'string', 'null' => true, 'default' => NULL, 'length' => 50, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'uri' => array('type' => 'string', 'null' => true, 'default' => NULL, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'refer' => array('type' => 'string', 'null' => true, 'default' => NULL, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'created' => array('type' => 'datetime', 'null' => true, 'default' => NULL),
'user_id' => array('type' => 'string', 'null' => true, 'default' => NULL, 'length' => 36, 'collate' => 'utf8mb4_general_ci', 'charset' => 'utf8mb4'),
'type' => array('type' => 'string', 'null' => false, 'default' => NULL, 'collate' => 'utf8mb4_general_ci', 'charset' => 'utf8mb4'),
'class' => array('type' => 'string', 'null' => false, 'default' => NULL, 'collate' => 'utf8mb4_general_ci', 'charset' => 'utf8mb4'),
'action' => array('type' => 'string', 'null' => false, 'default' => NULL, 'collate' => 'utf8mb4_general_ci', 'charset' => 'utf8mb4'),
'data' => array('type' => 'text', 'null' => false, 'default' => NULL, 'collate' => 'utf8mb4_general_ci', 'charset' => 'utf8mb4'),
'ip' => array('type' => 'string', 'null' => false, 'default' => NULL, 'collate' => 'utf8mb4_general_ci', 'charset' => 'utf8mb4'),
'httpcode' => array('type' => 'string', 'null' => false, 'default' => NULL, 'collate' => 'utf8mb4_general_ci', 'charset' => 'utf8mb4'),
'hostname' => array('type' => 'string', 'null' => true, 'default' => NULL, 'length' => 50, 'collate' => 'utf8mb4_general_ci', 'charset' => 'utf8mb4'),
'uri' => array('type' => 'string', 'null' => true, 'default' => NULL, 'collate' => 'utf8mb4_general_ci', 'charset' => 'utf8mb4'),
'refer' => array('type' => 'string', 'null' => true, 'default' => NULL, 'collate' => 'utf8mb4_general_ci', 'charset' => 'utf8mb4'),
'message' => array('type' => 'text', 'null' => false, 'default' => NULL, 'collate' => 'utf8mb4_general_ci', 'charset' => 'utf8mb4'),
'created' => array('type' => 'datetime', 'null' => false, 'default' => NULL),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1)),
'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_general_ci', 'engine' => 'MyISAM')
'tableParameters' => array('charset' => 'utf8mb4', 'collate' => 'utf8mb4_general_ci', 'engine' => 'InnoDB')
);
}
?>
?>
23 changes: 18 additions & 5 deletions libs/log/database_logger.php
Expand Up @@ -40,10 +40,23 @@ function __construct($options = array()){
* Write the log to database
*/
function write($type, $message){
$this->Log->save(array(
'type' => $type,
'message' => $message
));
$data = array();

if(is_array($message)) {
if(is_object($message[0])) {
$obj = $message[0];
$data['class'] = $obj->name;
$data['action'] = $obj->action;
$data['data'] = print_r($obj->data,true);
$data['user_id'] = $obj->Auth->User('id');
$data['message'] = $message[1];
}
} else {
$data['type'] = $type;
$data['message'] = $message;
}

$this->Log->save($data);
}
}
?>
?>
1 change: 1 addition & 0 deletions models/log.php
Expand Up @@ -9,6 +9,7 @@ function beforeSave(){
$this->data[$this->alias]['hostname'] = env('HTTP_HOST');
$this->data[$this->alias]['uri'] = env('REQUEST_URI');
$this->data[$this->alias]['refer'] = env('HTTP_REFERER');
$this->data[$this->alias]['httpcode'] = env('REDIRECT_STATUS');
return true;
}
}

0 comments on commit 06397d5

Please sign in to comment.