Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | 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\Formatter;
13
 
14
/**
15
 * formats the record to be used in the FlowdockHandler
16
 *
17
 * @author Dominik Liebler <liebler.dominik@gmail.com>
621 lars 18
 * @deprecated Since 2.9.0 and 3.3.0, Flowdock was shutdown we will thus drop this handler in Monolog 4
148 lars 19
 */
20
class FlowdockFormatter implements FormatterInterface
21
{
22
    /**
23
     * @var string
24
     */
25
    private $source;
26
 
27
    /**
28
     * @var string
29
     */
30
    private $sourceEmail;
31
 
32
    public function __construct(string $source, string $sourceEmail)
33
    {
34
        $this->source = $source;
35
        $this->sourceEmail = $sourceEmail;
36
    }
37
 
38
    /**
39
     * {@inheritDoc}
40
     *
41
     * @return mixed[]
42
     */
43
    public function format(array $record): array
44
    {
45
        $tags = [
46
            '#logs',
47
            '#' . strtolower($record['level_name']),
48
            '#' . $record['channel'],
49
        ];
50
 
51
        foreach ($record['extra'] as $value) {
52
            $tags[] = '#' . $value;
53
        }
54
 
55
        $subject = sprintf(
56
            'in %s: %s - %s',
57
            $this->source,
58
            $record['level_name'],
59
            $this->getShortMessage($record['message'])
60
        );
61
 
62
        $record['flowdock'] = [
63
            'source' => $this->source,
64
            'from_address' => $this->sourceEmail,
65
            'subject' => $subject,
66
            'content' => $record['message'],
67
            'tags' => $tags,
68
            'project' => $this->source,
69
        ];
70
 
71
        return $record;
72
    }
73
 
74
    /**
75
     * {@inheritDoc}
76
     *
77
     * @return mixed[][]
78
     */
79
    public function formatBatch(array $records): array
80
    {
81
        $formatted = [];
82
 
83
        foreach ($records as $record) {
84
            $formatted[] = $this->format($record);
85
        }
86
 
87
        return $formatted;
88
    }
89
 
90
    public function getShortMessage(string $message): string
91
    {
92
        static $hasMbString;
93
 
94
        if (null === $hasMbString) {
95
            $hasMbString = function_exists('mb_strlen');
96
        }
97
 
98
        $maxLength = 45;
99
 
100
        if ($hasMbString) {
101
            if (mb_strlen($message, 'UTF-8') > $maxLength) {
102
                $message = mb_substr($message, 0, $maxLength - 4, 'UTF-8') . ' ...';
103
            }
104
        } else {
105
            if (strlen($message) > $maxLength) {
106
                $message = substr($message, 0, $maxLength - 4) . ' ...';
107
            }
108
        }
109
 
110
        return $message;
111
    }
112
}