1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the overtrue/cuttle. |
5
|
|
|
* |
6
|
|
|
* (c) overtrue <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overtrue\Cuttle; |
13
|
|
|
|
14
|
|
|
use Monolog\Logger; |
15
|
|
|
use Monolog\Registry; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Cuttle. |
19
|
|
|
* |
20
|
|
|
* @author overtrue <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class Cuttle |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var \Overtrue\Cuttle\Config |
26
|
|
|
*/ |
27
|
|
|
protected $config; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Cuttle constructor. |
31
|
|
|
* |
32
|
|
|
* @param array $config |
33
|
|
|
*/ |
34
|
|
|
public function __construct(array $config) |
35
|
|
|
{ |
36
|
|
|
$this->config = new Config($config); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $name |
41
|
|
|
* |
42
|
|
|
* @return \Monolog\Logger |
43
|
|
|
*/ |
44
|
|
|
public function channel(string $name) |
45
|
|
|
{ |
46
|
|
|
return $this->getLogger($name); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $name |
51
|
|
|
* |
52
|
|
|
* @return \Monolog\Logger |
53
|
|
|
*/ |
54
|
|
|
public function of(string $name) |
55
|
|
|
{ |
56
|
|
|
return $this->channel($name); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $name |
61
|
|
|
* |
62
|
|
|
* @return \Monolog\Logger |
63
|
|
|
*/ |
64
|
|
|
public function from(string $name) |
65
|
|
|
{ |
66
|
|
|
return $this->channel($name); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return \Overtrue\Cuttle\Config |
71
|
|
|
*/ |
72
|
|
|
public function getConfig() |
73
|
|
|
{ |
74
|
|
|
return $this->config; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param \Overtrue\Cuttle\Config $config |
79
|
|
|
* |
80
|
|
|
* @return \Overtrue\Cuttle\Cuttle |
81
|
|
|
*/ |
82
|
|
|
public function setConfig(Config $config) |
83
|
|
|
{ |
84
|
|
|
$this->config = $config; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param $name |
91
|
|
|
* |
92
|
|
|
* @return \Monolog\Logger |
93
|
|
|
*/ |
94
|
|
|
public function getLogger(string $name) |
95
|
|
|
{ |
96
|
|
|
if (!Registry::hasLogger($name)) { |
97
|
|
|
$config = $this->config->getChannel($name); |
98
|
|
|
Registry::addLogger($this->makeLogger($name, $config['handlers'], $config['processors'])); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return Registry::getInstance($name); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return \Monolog\Logger |
106
|
|
|
*/ |
107
|
|
|
public function getDefaultLogger() |
108
|
|
|
{ |
109
|
|
|
return $this->getLogger($this->config->getDefaultChannel()); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $name |
114
|
|
|
* @param array $handlers |
115
|
|
|
* @param array $processors |
116
|
|
|
* |
117
|
|
|
* @return \Monolog\Logger |
118
|
|
|
*/ |
119
|
|
|
public function makeLogger(string $name, array $handlers = [], array $processors = []) |
120
|
|
|
{ |
121
|
|
|
return new Logger($name, $handlers, $processors); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Magic call. |
126
|
|
|
* |
127
|
|
|
* @param string $method |
128
|
|
|
* @param array $args |
129
|
|
|
* |
130
|
|
|
* @return mixed |
131
|
|
|
*/ |
132
|
|
|
public function __call($method, $args) |
133
|
|
|
{ |
134
|
|
|
$logger = $this->getDefaultLogger(); |
135
|
|
|
|
136
|
|
|
return call_user_func_array([$logger, $method], $args); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|