Completed
Pull Request — master (#18)
by yuuki
04:02
created

FluentHandler::write()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 12
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
10
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
11
 * THE SOFTWARE.
12
 *
13
 * This software consists of voluntary contributions made by many individuals
14
 * and is licensed under the MIT license.
15
 *
16
 * Copyright (c) 2015-2017 Yuuki Takezawa
17
 *
18
 */
19
20
namespace Ytake\LaravelFluent;
21
22
use Fluent\Logger\LoggerInterface;
23
use Monolog\Handler\AbstractProcessingHandler;
24
use Monolog\Logger;
25
26
/**
27
 * Class FluentHandler
28
 */
29
class FluentHandler extends AbstractProcessingHandler
30
{
31
    /** @var LoggerInterface */
32
    protected $logger;
33
34
    /** @var string */
35
    protected $tagFormat = '{{channel}}.{{level_name}}';
36
37
    /**
38
     * FluentHandler constructor.
39
     *
40
     * @param LoggerInterface $logger
41
     * @param null|string     $tagFormat
42
     * @param int             $level
43
     * @param bool            $bubble
44
     */
45
    public function __construct(
46
        LoggerInterface $logger,
47
        string $tagFormat = null,
48
        int $level = Logger::DEBUG,
49
        bool $bubble = true
50
    ) {
51
        $this->logger = $logger;
52
        if ($tagFormat !== null) {
53
            $this->tagFormat = $tagFormat;
54
        }
55
        parent::__construct($level, $bubble);
56
    }
57
58
    /**
59
     * @param array $record
60
     */
61
    protected function write(array $record)
62
    {
63
        $tag = $this->populateTag($record);
64
        $this->logger->post(
65
            $tag,
66
            [
67
                'message' => $record['message'],
68
                'context' => $record['context'],
69
                'extra'   => $record['extra'],
70
            ]
71
        );
72
    }
73
74
    /**
75
     * @param array $record
76
     *
77
     * @return string
78
     */
79
    protected function populateTag(array $record): string
80
    {
81
        return $this->processFormat($record, $this->tagFormat);
82
    }
83
84
    /**
85
     * @param array  $record
86
     * @param string $tag
87
     *
88
     * @return string
89
     */
90
    protected function processFormat(array $record, string $tag): string
91
    {
92
        if (preg_match_all('/\{\{(.*?)\}\}/', $tag, $matches)) {
93
            foreach ($matches[1] as $match) {
94
                if (!isset($record[$match])) {
95
                    throw new \LogicException('No such field in the record');
96
                }
97
                $tag = str_replace(sprintf('{{%s}}', $match), $record[$match], $tag);
98
            }
99
        }
100
101
        return $tag;
102
    }
103
104
    /**
105
     * @return LoggerInterface
106
     */
107
    public function getLogger(): LoggerInterface
108
    {
109
        return $this->logger;
110
    }
111
}
112