1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Monolog Extensions |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @see http://github.com/graze/MonologExtensions/blob/master/LICENSE |
11
|
|
|
* @link http://github.com/graze/MonologExtensions |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\Monolog; |
15
|
|
|
|
16
|
|
|
use Graze\Monolog\Processor\EnvironmentProcessor; |
17
|
|
|
use Graze\Monolog\Processor\ExceptionMessageProcessor; |
18
|
|
|
use Graze\Monolog\Processor\HttpProcessor; |
19
|
|
|
use Monolog\Handler\HandlerInterface; |
20
|
|
|
use Monolog\Logger; |
21
|
|
|
|
22
|
|
|
class LoggerBuilder |
23
|
|
|
{ |
24
|
|
|
const DEFAULT_NAME = 'error'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var HandlerInterface[] |
28
|
|
|
*/ |
29
|
|
|
protected $handlers = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $name; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Callable[] |
38
|
|
|
*/ |
39
|
|
|
protected $processors = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return Logger |
43
|
|
|
*/ |
44
|
3 |
|
public function build() |
45
|
|
|
{ |
46
|
3 |
|
return new Logger( |
47
|
3 |
|
$this->getName() ?: static::DEFAULT_NAME, |
48
|
3 |
|
$this->getHandlers() ?: $this->getDefaultHandlers(), |
49
|
3 |
|
$this->getProcessors() ?: $this->getDefaultProcessors() |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
5 |
|
public function getName() |
57
|
|
|
{ |
58
|
5 |
|
return $this->name; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $name |
63
|
|
|
* |
64
|
|
|
* @return LoggerBuilder |
65
|
|
|
*/ |
66
|
1 |
|
public function setName($name) |
67
|
|
|
{ |
68
|
1 |
|
$this->name = $name; |
69
|
|
|
|
70
|
1 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param HandlerInterface $handler |
75
|
|
|
* |
76
|
|
|
* @return LoggerBuilder |
77
|
|
|
*/ |
78
|
2 |
|
public function addHandler(HandlerInterface $handler) |
79
|
|
|
{ |
80
|
2 |
|
$this->handlers[] = $handler; |
81
|
|
|
|
82
|
2 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return HandlerInterface[] |
87
|
|
|
*/ |
88
|
6 |
|
public function getHandlers() |
89
|
|
|
{ |
90
|
6 |
|
return $this->handlers; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param HandlerInterface[] $handlers |
95
|
|
|
* |
96
|
|
|
* @return LoggerBuilder |
97
|
|
|
*/ |
98
|
1 |
|
public function setHandlers(array $handlers) |
99
|
|
|
{ |
100
|
1 |
|
$this->handlers = []; |
101
|
|
|
|
102
|
1 |
|
foreach ($handlers as $handler) { |
103
|
1 |
|
$this->addHandler($handler); |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param callable $processor |
111
|
|
|
* |
112
|
|
|
* @return LoggerBuilder |
113
|
|
|
*/ |
114
|
2 |
|
public function addProcessor(callable $processor) |
115
|
|
|
{ |
116
|
2 |
|
$this->processors[] = $processor; |
117
|
|
|
|
118
|
2 |
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return Callable[] |
123
|
|
|
*/ |
124
|
6 |
|
public function getProcessors() |
125
|
|
|
{ |
126
|
6 |
|
return $this->processors; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param callable[] $processors |
131
|
|
|
* |
132
|
|
|
* @return LoggerBuilder |
133
|
|
|
*/ |
134
|
1 |
|
public function setProcessors(array $processors) |
135
|
|
|
{ |
136
|
1 |
|
$this->processors = []; |
137
|
|
|
|
138
|
1 |
|
foreach ($processors as $processor) { |
139
|
1 |
|
$this->addProcessor($processor); |
140
|
|
|
} |
141
|
|
|
|
142
|
1 |
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return HandlerInterface[] |
147
|
|
|
*/ |
148
|
3 |
|
protected function getDefaultHandlers() |
149
|
|
|
{ |
150
|
3 |
|
return []; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return Callable[] |
155
|
|
|
*/ |
156
|
3 |
|
protected function getDefaultProcessors() |
157
|
|
|
{ |
158
|
|
|
return [ |
159
|
3 |
|
new ExceptionMessageProcessor(), |
160
|
3 |
|
new EnvironmentProcessor(), |
161
|
3 |
|
new HttpProcessor(), |
162
|
|
|
]; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|