1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
5
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
6
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
7
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
8
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
9
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
10
|
|
|
* THE SOFTWARE. |
11
|
|
|
* |
12
|
|
|
* This software consists of voluntary contributions made by many individuals |
13
|
|
|
* and is licensed under the MIT license. |
14
|
|
|
* Copyright (c) 2015 Yuuki Takezawa |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace Ytake\LaravelFluent; |
18
|
|
|
|
19
|
|
|
use Fluent\Logger\LoggerInterface; |
20
|
|
|
use Monolog\Logger; |
21
|
|
|
use Monolog\Handler\AbstractProcessingHandler; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class FluentHandler |
25
|
|
|
* |
26
|
|
|
* @package Ytake\LaravelFluent |
27
|
|
|
*/ |
28
|
|
|
class FluentHandler extends AbstractProcessingHandler |
29
|
|
|
{ |
30
|
|
|
/** @var LoggerInterface */ |
31
|
|
|
protected $logger; |
32
|
|
|
|
33
|
|
|
/** @var string */ |
34
|
|
|
protected $tagFormat = '{{channel}}.{{level_name}}'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* FluentHandler constructor. |
38
|
|
|
* |
39
|
|
|
* @param LoggerInterface $logger |
40
|
|
|
* @param bool|int $level |
41
|
|
|
* @param bool|true $bubble |
42
|
|
|
*/ |
43
|
5 |
|
public function __construct(LoggerInterface $logger, $tagFormat = null, $level = Logger::DEBUG, $bubble = true) |
44
|
|
|
{ |
45
|
5 |
|
$this->logger = $logger; |
46
|
5 |
|
if ($tagFormat !== null) { |
47
|
|
|
$this->tagFormat = $tagFormat; |
48
|
|
|
} |
49
|
5 |
|
parent::__construct($level, $bubble); |
|
|
|
|
50
|
5 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param array $record |
54
|
|
|
*/ |
55
|
1 |
|
protected function write(array $record) |
56
|
|
|
{ |
57
|
|
|
//sprintf($this->tagFormat, $record['channel'], $record['level_name']) |
|
|
|
|
58
|
1 |
|
$tag = $this->populateTag($record); |
59
|
1 |
|
$this->logger->post( |
60
|
1 |
|
$tag, |
|
|
|
|
61
|
1 |
|
[$record['message'] => $record['context']] |
62
|
1 |
|
); |
63
|
1 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param array $record |
67
|
|
|
*/ |
68
|
1 |
|
protected function populateTag($record) |
69
|
|
|
{ |
70
|
1 |
|
return $this->processFormat($record, $this->tagFormat); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param array $record |
75
|
|
|
* @param array $format |
76
|
|
|
*/ |
77
|
1 |
|
protected function processFormat($record, $format) |
78
|
|
|
{ |
79
|
1 |
|
$tag = $format; |
80
|
|
|
|
81
|
1 |
|
if (preg_match_all('/\{\{(.*?)\}\}/', $tag, $matches)) { |
82
|
1 |
|
foreach ($matches[1] as $match) { |
83
|
1 |
|
if (!isset($record[$match])) { |
84
|
|
|
throw new Exception('No such field in the record'); |
85
|
|
|
} |
86
|
1 |
|
$tag = str_replace(sprintf('{{%s}}', $match), $record[$match], $tag); |
87
|
1 |
|
} |
88
|
1 |
|
} |
89
|
|
|
|
90
|
1 |
|
return $tag; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return LoggerInterface |
95
|
|
|
*/ |
96
|
1 |
|
public function getLogger() |
97
|
|
|
{ |
98
|
1 |
|
return $this->logger; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.