1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the feed-io package. |
4
|
|
|
* |
5
|
|
|
* (c) Alexandre Debril <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace FeedIo\Factory\Builder; |
12
|
|
|
|
13
|
|
|
use FeedIo\Factory\LoggerBuilderInterface; |
14
|
|
|
use Monolog\Logger; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @package FeedIo |
18
|
|
|
*/ |
19
|
|
|
class MonologBuilder implements LoggerBuilderInterface |
20
|
|
|
{ |
21
|
|
|
protected $loggerName = 'feed-io'; |
22
|
|
|
|
23
|
|
|
protected $handlersConfig = [ |
24
|
|
|
[ |
25
|
|
|
'class' => 'Monolog\Handler\StreamHandler', |
26
|
|
|
'params' => ['php://stdout', Logger::DEBUG], |
27
|
|
|
], |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array $config |
32
|
|
|
*/ |
33
|
8 |
|
public function __construct(array $config = []) |
34
|
|
|
{ |
35
|
8 |
|
$this->loggerName = isset($config['name']) ? $config['name']:$this->loggerName; |
36
|
|
|
|
37
|
8 |
|
$this->handlersConfig = isset($config['handlers']) ? $config['handlers']:$this->handlersConfig; |
38
|
8 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* This method MUST return a valid PSR3 logger |
42
|
|
|
* @return \Monolog\Logger |
43
|
|
|
*/ |
44
|
2 |
|
public function getLogger() |
45
|
|
|
{ |
46
|
2 |
|
$logger = new Logger($this->loggerName); |
47
|
|
|
|
48
|
2 |
|
foreach ($this->handlersConfig as $config) { |
49
|
2 |
|
$handler = $this->newHandler($config['class'], $config['params']); |
50
|
2 |
|
$logger->pushHandler($handler); |
51
|
2 |
|
} |
52
|
|
|
|
53
|
2 |
|
return $logger; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $class |
58
|
|
|
* @param array $params |
59
|
|
|
* @return Monolog\Handler\HandlerInterface |
60
|
|
|
*/ |
61
|
4 |
|
public function newHandler($class, array $params = []) |
62
|
|
|
{ |
63
|
4 |
|
$reflection = new \ReflectionClass($class); |
64
|
|
|
|
65
|
4 |
|
if (! $reflection->implementsInterface('Monolog\Handler\HandlerInterface')) { |
66
|
1 |
|
throw new \InvalidArgumentException(); |
67
|
|
|
} |
68
|
|
|
|
69
|
3 |
|
return $reflection->newInstanceArgs($params); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* This method MUST return the name of the main class |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
1 |
|
public function getMainClassName() |
77
|
|
|
{ |
78
|
1 |
|
return 'Monolog\Logger'; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* This method MUST return the name of the package name |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
1 |
|
public function getPackageName() |
86
|
|
|
{ |
87
|
1 |
|
return 'monolog/monolog'; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|