~thirdplace/components

ref: 8ce6124fd114046c612c23f8803f897a94443a82 components/src/logger/StreamHandler.php -rwxr-xr-x 1.5 KiB
8ce6124fDag yup 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
declare(strict_types=1);

namespace Thirdplace;

final class StreamHandler
{
    private $stream;

    public function __construct($stream)
    {
        $this->stream = $stream;
    }

    public function __invoke(array $record): void
    {
        if (isset($record['context']['e'])) {
            // todo: improve message
            $record['context']['message'] = $e->getMessage();
            $record['context']['code'] = $e->getCode();
            $record['context']['trace'] = create_sane_stacktrace($record['context']['e']);
        }
        if ($record['context'] === []) {
            $record['context'] = '';
        } else {
            $json = Json::encode($record['context']) ?: '["Unable to json encode context"]';
            $record['context'] = $json;
        }

        $text = sprintf(
            "[%s] %s.%s %s %s\n",
            $record['created_at']->format('Y-m-d H:i:s'),
            $record['name'],
            $record['level_name'],
            // todo: replace non-visible characters too such as null byte etc.
            str_replace(["\n", "\r"], '\n', $record['message']),
            $record['context']
        );

        if (! is_resource($this->stream)) {
            $this->stream = fopen($this->stream, 'a');
        }

        if (!fwrite($this->stream, $text)) {
            // Maybe drop this last effort to write to error log
            if (!error_log('Unable to write log record: ' . $record['message']])) {
                // todo: write to stderr or stdout?
            }
        }
    }
}