Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
2
 
3
/*
4
 * This file is part of the Monolog package.
5
 *
6
 * (c) Jordi Boggiano <j.boggiano@seld.be>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Monolog\Handler;
13
 
14
use Monolog\Logger;
15
use Psr\Log\LoggerInterface;
16
use Monolog\Formatter\FormatterInterface;
17
 
18
/**
19
 * Proxies log messages to an existing PSR-3 compliant logger.
20
 *
21
 * If a formatter is configured, the formatter's output MUST be a string and the
22
 * formatted message will be fed to the wrapped PSR logger instead of the original
23
 * log record's message.
24
 *
25
 * @author Michael Moussa <michael.moussa@gmail.com>
26
 */
27
class PsrHandler extends AbstractHandler implements FormattableHandlerInterface
28
{
29
    /**
30
     * PSR-3 compliant logger
31
     *
32
     * @var LoggerInterface
33
     */
34
    protected $logger;
35
 
36
    /**
37
     * @var FormatterInterface|null
38
     */
39
    protected $formatter;
40
 
41
    /**
42
     * @param LoggerInterface $logger The underlying PSR-3 compliant logger to which messages will be proxied
43
     */
44
    public function __construct(LoggerInterface $logger, $level = Logger::DEBUG, bool $bubble = true)
45
    {
46
        parent::__construct($level, $bubble);
47
 
48
        $this->logger = $logger;
49
    }
50
 
51
    /**
52
     * {@inheritDoc}
53
     */
54
    public function handle(array $record): bool
55
    {
56
        if (!$this->isHandling($record)) {
57
            return false;
58
        }
59
 
60
        if ($this->formatter) {
61
            $formatted = $this->formatter->format($record);
62
            $this->logger->log(strtolower($record['level_name']), (string) $formatted, $record['context']);
63
        } else {
64
            $this->logger->log(strtolower($record['level_name']), $record['message'], $record['context']);
65
        }
66
 
67
        return false === $this->bubble;
68
    }
69
 
70
    /**
71
     * Sets the formatter.
72
     *
73
     * @param FormatterInterface $formatter
74
     */
75
    public function setFormatter(FormatterInterface $formatter): HandlerInterface
76
    {
77
        $this->formatter = $formatter;
78
 
79
        return $this;
80
    }
81
 
82
    /**
83
     * Gets the formatter.
84
     *
85
     * @return FormatterInterface
86
     */
87
    public function getFormatter(): FormatterInterface
88
    {
89
        if (!$this->formatter) {
90
            throw new \LogicException('No formatter has been set and this handler does not have a default formatter');
91
        }
92
 
93
        return $this->formatter;
94
    }
95
}