• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

fractalzombie / frzb-metrics-power / 9029605458

10 May 2024 08:35AM UTC coverage: 90.119% (-9.0%) from 99.111%
9029605458

push

github

fractalzombie
[MetricsPower] Added logstash processor

12 of 35 new or added lines in 4 files covered. (34.29%)

228 of 253 relevant lines covered (90.12%)

10.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/Logger/Processor/LogstashProcessor.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
7
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
8
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
9
 *
10
 * Copyright (c) 2024 Mykhailo Shtanko fractalzombie@gmail.com
11
 *
12
 * For the full copyright and license information, please view the LICENSE.MD
13
 * file that was distributed with this source code.
14
 */
15

16
namespace FRZB\Component\MetricsPower\Logger\Processor;
17

18
use FRZB\Component\DependencyInjection\Attribute\AsTagged;
19
use Monolog\LogRecord;
20
use Symfony\Component\DependencyInjection\Attribute\Autowire;
21

22
#[AsTagged('monolog.processor')]
23
class LogstashProcessor
24
{
25
    public function __construct(
26
        #[Autowire(env: 'MONOLOG_SOURCE')]
27
        private readonly string $source,
NEW
28
    ) {}
×
29

30
    public function __invoke(LogRecord $record): LogRecord
31
    {
NEW
32
        return new LogRecord(
×
NEW
33
            $record->datetime,
×
NEW
34
            $record->channel,
×
NEW
35
            $record->level,
×
NEW
36
            json_validate($record->message) ? $this->formatJson($record->message) : $record->message,
×
NEW
37
            $this->mapContext($record),
×
NEW
38
            $this->mapExtra($record),
×
NEW
39
            json_validate($record->formatted) ? $this->formatJson($record->formatted) : $record->formatted,
×
NEW
40
        );
×
41
    }
42

43
    private function formatJson(string $json): string
44
    {
NEW
45
        $decodedJson = json_decode($json, true);
×
46

NEW
47
        return json_encode($decodedJson, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE);
×
48
    }
49

50
    private function mapExtra(LogRecord $record): array
51
    {
NEW
52
        return [
×
NEW
53
            ...$record->extra,
×
NEW
54
            'source' => $this->source,
×
NEW
55
        ];
×
56
    }
57

58
    private function mapContext(LogRecord $record): array
59
    {
NEW
60
        return [
×
NEW
61
            ...$record->context,
×
NEW
62
        ];
×
63
    }
64
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc