MonologCollector::write()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace LeKoala\DebugBar\Bridge;
4
5
use DebugBar\DataCollector\DataCollectorInterface;
6
use DebugBar\DataCollector\MessagesAggregateInterface;
7
use DebugBar\DataCollector\Renderable;
8
use Monolog\Handler\AbstractProcessingHandler;
9
use Monolog\Logger;
10
use Monolog\LogRecord;
11
12
/**
13
 * A monolog handler as well as a data collector. Based on DebugBar\Bridge\MonologCollector
14
 * Note: This class is a temporary solution to keep existing dependencies working.
15
 * As soon as maximebf/php-debugbar is updated and compatible with monolog/monolog:^3
16
 * this class should be deprecated and the DebugBar\Bridge\MonologCollector class should be used instead of this
17
 * <code>
18
 * $debugbar->addCollector(new MonologCollector($logger));
19
 * </code>
20
 */
21
class MonologCollector extends AbstractProcessingHandler implements DataCollectorInterface, Renderable, MessagesAggregateInterface
22
{
23
    protected $name;
24
25
    protected $records = [];
26
27
    /**
28
     * @param Logger $logger
29
     * @param int $level
30
     * @param boolean $bubble
31
     * @param string $name
32
     */
33
    public function __construct(?Logger $logger = null, $level = Logger::DEBUG, $bubble = true, $name = 'monolog')
0 ignored issues
show
Deprecated Code introduced by
The constant Monolog\Logger::DEBUG has been deprecated: Use \Monolog\Level::Debug ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

33
    public function __construct(?Logger $logger = null, $level = /** @scrutinizer ignore-deprecated */ Logger::DEBUG, $bubble = true, $name = 'monolog')

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
34
    {
35
        parent::__construct($level, $bubble);
36
        $this->name = $name;
37
        if ($logger !== null) {
38
            $this->addLogger($logger);
39
        }
40
    }
41
42
    /**
43
     * Adds logger which messages you want to log
44
     *
45
     * @param Logger $logger
46
     */
47
    public function addLogger(Logger $logger)
48
    {
49
        $logger->pushHandler($this);
50
    }
51
52
    protected function write(LogRecord $record): void
53
    {
54
        $this->records[] = [
55
            'message' => $record['formatted'],
56
            'is_string' => true,
57
            'label' => strtolower($record['level_name']),
58
            'time' => $record['datetime']->format('U')
59
        ];
60
    }
61
62
    /**
63
     * @return array
64
     */
65
    public function getMessages()
66
    {
67
        return $this->records;
68
    }
69
70
    /**
71
     * @return array
72
     */
73
    public function collect()
74
    {
75
        return [
76
            'count' => count($this->records),
77
            'records' => $this->records
78
        ];
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function getName()
85
    {
86
        return $this->name;
87
    }
88
89
    /**
90
     * @return array
91
     */
92
    public function getWidgets()
93
    {
94
        $name = $this->getName();
95
        return [
96
            $name => [
97
                "icon" => "suitcase",
98
                "widget" => "PhpDebugBar.Widgets.MessagesWidget",
99
                "map" => "$name.records",
100
                "default" => "[]"
101
            ],
102
            "$name:badge" => [
103
                "map" => "$name.count",
104
                "default" => "null"
105
            ]
106
        ];
107
    }
108
}
109