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

The-oGlow / ezlogging / 18278713918

06 Oct 2025 11:02AM UTC coverage: 84.295% (+2.2%) from 82.105%
18278713918

push

github

web-flow
Merge pull request #10 from The-oGlow/develop

Develop

148 of 175 new or added lines in 13 files covered. (84.57%)

12 existing lines in 2 files now uncovered.

263 of 312 relevant lines covered (84.29%)

13.63 hits per line

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

94.74
/src/Monolog/Handler/CsvHandler.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of ezlogging
7
 *
8
 * (c) 2025 Oliver Glowa, coding.glowa.com
9
 *
10
 * This source file is subject to the Apache-2.0 license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13

14
namespace Monolog\Handler;
15

16
use Monolog\FileLogger;
17
use Monolog\Formatter\FormatterInterface;
18
use Monolog\Formatter\LineFormatter;
19
use Monolog\Formatter\NormalizerFormatter;
20
use Monolog\Handler\StreamHandler;
21
use ollily\Tools\PhpVersionTrait;
22
use ollily\Tools\String\ImplodeTrait;
23

24
/**
25
 * Stores any data (given as array) to a csv file.
26
 *
27
 * Inspired by {@see https://github.com/femtopixel/monolog-csvhandler}.
28
 * Original by @author Jay MOULIN <jay@femtopixel.com>
29
 */
30
class CsvHandler extends FileHandler
31
{
32
    use PhpVersionTrait;
33
    use ImplodeTrait;
34

35
    /** @var string */
36
    public const    STANDARD_FILENAME = 'noCSVName';
37

38
    /** @var string */
39
    public const    STANDARD_FILEEXT     = '.csv';
40

41
    public const    STANDARD_ITEM_SEP    = ';';
42

43
    public const    STANDARD_TEXT_SEP    = '"';
44

45
    public const    STANDARD_ESCAPE_CHAR = '\\';
46

47
    protected const KEY_FORMATTED        = 'formatted';
48

49
    /** @var string */
50
    private $itemSeparator;
51

52
    /** @var string */
53
    private $itemEnclosure;
54

55
    /**
56
     * CsvHandler constructor.
57
     *
58
     * @param string $pathToFile
59
     * @param string $fileName
60
     * @param string $itemSeparator
61
     * @param string $itemEnclosure
62
     */
63
    public function __construct(
22✔
64
        ?string $pathToFile = null,
65
        ?string $fileName = null,
66
        string $itemSeparator = self::STANDARD_ITEM_SEP,
67
        string $itemEnclosure = self::STANDARD_TEXT_SEP
68
    ) {
69
        parent::__construct(
22✔
70
            $pathToFile,
22✔
71
            $fileName
22✔
72
        );
22✔
73
        $this->itemSeparator = $itemSeparator;
22✔
74
        $this->itemEnclosure = $itemEnclosure;
22✔
75
    }
76

77
    /**
78
     * @inheritdoc
79
     */
80
    protected function streamWrite($stream, array $record): void
13✔
81
    {
82
        $output = [];
13✔
83
        // @phpstan-ignore isset.offset
84
        if (isset($record[self::KEY_MESSAGE]) && !empty($record[self::KEY_MESSAGE])) {
13✔
85
            array_push($output, $record[self::KEY_MESSAGE]);
10✔
86
        }
87

88
        // @phpstan-ignore isset.offset
89
        if (isset($record[self::KEY_CONTEXT]) && !empty($record[self::KEY_CONTEXT])) {
13✔
90
            $implodeContext = $record[self::KEY_CONTEXT];
10✔
91
            /**
92
             * @psalm-suppress RedundantCondition
93
             * @phpstan-ignore if.alwaysTrue
94
             */
95
            if (is_array($record[self::KEY_CONTEXT])) {
10✔
96
                $implodeContext = $this->array_flatten($record[self::KEY_CONTEXT]);
10✔
97
            }
98
            $output = array_merge($output, $implodeContext);
10✔
99
        }
100
        if ($this->isPhpGreater('5.5.4')) {
13✔
101
            fputcsv($stream, $output, $this->itemSeparator, $this->itemEnclosure, static::STANDARD_ESCAPE_CHAR);
13✔
102
        } else {
NEW
103
            fputcsv($stream, $output, $this->itemSeparator, $this->itemEnclosure);
×
104
        }
105
    }
106
}
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