Permalink
Browse files

Refactored Error Handling

  • Loading branch information...
1 parent b7b6363 commit 06397d59ec888ebea1a9e350a8074f02e10de3a3 @rnavarro committed Sep 21, 2011
Showing with 34 additions and 15 deletions.
  1. +15 −10 config/schema/schema.php
  2. +18 −5 libs/log/database_logger.php
  3. +1 −0 models/log.php
View
@@ -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';
@@ -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')
);
}
-?>
+?>
@@ -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);
}
}
-?>
+?>
View
@@ -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.