1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverLeague\LogViewer\Handler; |
4
|
|
|
|
5
|
|
|
use Monolog\Logger; |
6
|
|
|
use Monolog\Formatter\JsonFormatter; |
7
|
|
|
use Monolog\Handler\AbstractProcessingHandler; |
8
|
|
|
use SilverLeague\LogViewer\Model\LogEntry; |
9
|
|
|
use SilverStripe\Core\Config\Config; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* The DataObjectHandler allows you to use a SilverStripe DataObject for handling Monolog log entries. |
13
|
|
|
* |
14
|
|
|
* The default class to use is "LogEntry" which will store the log message, level and date/time in the database. |
15
|
|
|
* |
16
|
|
|
* @package silverstripe-logviewer |
17
|
|
|
* @author Robbie Averill <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class DataObjectHandler extends AbstractProcessingHandler |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* The default DataObject to use for storing log entries |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
const DEFAULT_CLASS = LogEntry::class; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The default DateTime format to use for storing the log timestamp |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
const DEFAULT_DATETIME_FORMAT = 'Y-m-d H:i:s'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* DataObject class for storing log entries |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $objectClass; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $objectClass The DataObject class to use for handling the write |
44
|
|
|
* @param int $level The minimum logging level at which this handler will be triggered (configurable) |
45
|
|
|
* @param boolean $bubble Whether the messages that are handled can bubble up the stack or not |
46
|
|
|
*/ |
47
|
|
|
public function __construct($objectClass = self::DEFAULT_CLASS, $level = Logger::DEBUG, $bubble = true) |
48
|
|
|
{ |
49
|
|
|
$this->setObjectClass($objectClass); |
50
|
|
|
$level = $this->getMinimumLogLevel(); |
51
|
|
|
parent::__construct($level, $bubble); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function getDefaultFormatter() |
55
|
|
|
{ |
56
|
|
|
return new JsonFormatter; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function write(array $record) |
60
|
|
|
{ |
61
|
|
|
$this->addDataObject((string) $record['formatted'], $record['level_name']); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Create a new DataObject instance and set the log information to it |
66
|
|
|
* |
67
|
|
|
* @param string $message The log message |
68
|
|
|
* @param string $level The log level text, e.g. "DEBUG" |
69
|
|
|
* @return int The written DataObject ID |
70
|
|
|
*/ |
71
|
|
|
public function addDataObject($message, $level) |
72
|
|
|
{ |
73
|
|
|
$class = $this->getObjectClass(); |
74
|
|
|
|
75
|
|
|
$object = $class::create(); |
76
|
|
|
$object->setField('Entry', $message); |
77
|
|
|
$object->setField('Level', $level); |
78
|
|
|
$object->write(); |
79
|
|
|
|
80
|
|
|
return $object->ID; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set the DataObject to use for storing log entries |
85
|
|
|
* |
86
|
|
|
* @param string $class |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
|
|
public function setObjectClass($class) |
90
|
|
|
{ |
91
|
|
|
$this->objectClass = $class; |
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get the DataObject to use for storing log entries |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function getObjectClass() |
101
|
|
|
{ |
102
|
|
|
return $this->objectClass; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the minimum Monolog\Logger log level to start catching messages at |
107
|
|
|
* |
108
|
|
|
* @see \Monolog\Logger |
109
|
|
|
* @return int |
110
|
|
|
*/ |
111
|
|
|
public function getMinimumLogLevel() |
112
|
|
|
{ |
113
|
|
|
return (int) Config::inst()->get(LogEntry::class, 'minimum_log_level'); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|