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
|
|
|
|
22
|
|
|
protected $loggerName = 'feed-io'; |
23
|
|
|
|
24
|
|
|
protected $handlersConfig = [ |
25
|
|
|
[ |
26
|
|
|
'class' => 'Monolog\Handler\StreamHandler', |
27
|
|
|
'params' => ['php://stdout', Logger::DEBUG], |
28
|
|
|
], |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array $config |
33
|
|
|
*/ |
34
|
7 |
|
public function __construct(array $config = []) |
35
|
|
|
{ |
36
|
7 |
|
$this->loggerName = isset($config['name']) ? $config['name']:$this->loggerName; |
37
|
|
|
|
38
|
7 |
|
$this->handlersConfig = isset($config['handlers']) ? $config['handlers']:$this->handlersConfig; |
39
|
7 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* This method MUST return a valid PSR3 logger |
43
|
|
|
* @return \Monolog\Logger |
44
|
|
|
*/ |
45
|
1 |
|
public function getLogger() |
46
|
|
|
{ |
47
|
1 |
|
$logger = new Logger($this->loggerName); |
48
|
|
|
|
49
|
1 |
|
foreach ( $this->handlersConfig as $config ) { |
50
|
1 |
|
$handler = $this->newHandler($config['class'], $config['params']); |
51
|
1 |
|
$logger->pushHandler($handler); |
52
|
1 |
|
} |
53
|
|
|
|
54
|
1 |
|
return $logger; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $class |
59
|
|
|
* @param array $params |
60
|
|
|
* @return Monolog\Handler\HandlerInterface |
61
|
|
|
*/ |
62
|
3 |
|
public function newHandler($class, array $params = []) |
63
|
|
|
{ |
64
|
3 |
|
$reflection = new \ReflectionClass($class); |
65
|
|
|
|
66
|
3 |
|
if ( ! $reflection->implementsInterface('Monolog\Handler\HandlerInterface') ) { |
67
|
1 |
|
throw new \InvalidArgumentException(); |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
return $reflection->newInstanceArgs($params); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* This method MUST return the name of the main class |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
1 |
|
public function getMainClassName() |
78
|
|
|
{ |
79
|
1 |
|
return 'Monolog\Logger'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* This method MUST return the name of the package name |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
1 |
|
public function getPackageName() |
87
|
|
|
{ |
88
|
1 |
|
return 'monolog/monolog'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|