summaryrefslogtreecommitdiffstats
path: root/vendor/gipfl/log/src/Writer/JsonRpcWriter.php
blob: a2fa50593d7de559a9b65e4513f5e8b9bd718554 (plain)
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
52
53
54
<?php

namespace gipfl\Log\Writer;

use gipfl\Log\LogWriterWithContext;
use gipfl\Protocol\JsonRpc\Connection;
use function iconv;
use function microtime;

/**
 * @deprecated
 */
class JsonRpcWriter implements LogWriterWithContext
{
    const DEFAULT_RPC_METHOD = 'logger.log';

    /** @var Connection */
    protected $connection;

    /** @var string */
    protected $method = self::DEFAULT_RPC_METHOD;

    /** @var array */
    protected $defaultContext;

    /**
     * JsonRpcWriter constructor.
     * @param Connection $connection
     */
    public function __construct(Connection $connection, $defaultContext = [])
    {
        $this->connection = $connection;
        $this->defaultContext = $defaultContext;
    }

    /**
     * @param string $method
     */
    public function setMethod($method)
    {
        $this->method = $method;
    }

    public function write($level, $message, $context = [])
    {
        $message = iconv('UTF-8', 'UTF-8//IGNORE', $message);
        $this->connection->notification($this->method, $this->defaultContext + [
            'level'     => $level,
            'timestamp' => microtime(true),
            'message'   => $message,
            'context'   => $context,
        ]);
    }
}