/
database_logger.php
60 lines (50 loc) · 1.36 KB
/
database_logger.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
if (!class_exists('ClassRegistry')) {
App::import('Core', 'ClassRegistry');
}
class DatabaseLogger extends Object {
var $model = null;
var $defaults = array(
'modelName' => 'Log',
);
/**
* Constructs a new DataBase Logger.
*
* Options
*
* - `modelName` the name of model to instantiate.
*
* @param array $options Options for the DataBaseLog, see above.
* @return void
*/
function DatabaseLogger($options = array()) {
$this->options = array_merge($this->defaults, (array) $options);
if (!isset($this->options['modelName'])) {
throw new InvalidArgumentException("Invalid modelname");
}
$this->model = ClassRegistry::init($this->options['modelName']);
}
/**
* Implements writing to log files.
*
* @param string $type The type of log you are making.
* @param string $message The message you want to log.
* @return boolean success of write.
*/
function write($type, $message) {
if (is_array($message)) {
$message = json_encode($message);
}
$ip = env('REMOTE_ADDR');
$browser = env('HTTP_USER_AGENT');
$hostname = env('HTTP_HOST');
$url = env('REQUEST_URI');
$refer = env('HTTP_REFERER');
$data = compact('type', 'message', 'ip', 'browser', 'hostname', 'url', 'referer');
if (class_exists('Authsome')) {
$data['user_id'] = Authsome::get('id');
}
$this->model->create(false);
return $this->model->save($data);
}
}