Completed
Pull Request — master (#221)
by
unknown
01:13
created

RPCLogger::debug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Spiral\RoadRunner;
4
5
use Psr\Log\InvalidArgumentException;
6
use Psr\Log\LoggerInterface;
7
use Psr\Log\LogLevel;
8
use Spiral\Goridge\RPC;
9
10
final class RPCLogger implements LoggerInterface
11
{
12
    private const PSR_TO_LOGRUS_LEVELS = [
13
        // levels above "error" causes logrus to os.exit() or panic()
14
        LogLevel::EMERGENCY => "error",
15
        LogLevel::ALERT => "error",
16
        LogLevel::CRITICAL => "error",
17
        LogLevel::ERROR => "error",
18
        LogLevel::WARNING => "warning",
19
        LogLevel::NOTICE => "info",
20
        LogLevel::INFO => "info",
21
        LogLevel::DEBUG => "debug",
22
    ];
23
24
    /**
25
     * @var RPC
26
     */
27
    private $rpc;
28
29
    public function __construct(RPC $rpc)
30
    {
31
        $this->rpc = $rpc;
32
    }
33
34
    /**
35
     * @inheritDoc
36
     */
37
    public function emergency($message, array $context = array())
38
    {
39
        $this->log(LogLevel::EMERGENCY, $message, $context);
40
    }
41
42
    /**
43
     * @inheritDoc
44
     */
45
    public function alert($message, array $context = array())
46
    {
47
        $this->log(LogLevel::ALERT, $message, $context);
48
    }
49
50
    /**
51
     * @inheritDoc
52
     */
53
    public function critical($message, array $context = array())
54
    {
55
        $this->log(LogLevel::CRITICAL, $message, $context);
56
    }
57
58
    /**
59
     * @inheritDoc
60
     */
61
    public function error($message, array $context = array())
62
    {
63
        $this->log(LogLevel::ERROR, $message, $context);
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69
    public function warning($message, array $context = array())
70
    {
71
        $this->log(LogLevel::WARNING, $message, $context);
72
    }
73
74
    /**
75
     * @inheritDoc
76
     */
77
    public function notice($message, array $context = array())
78
    {
79
        $this->log(LogLevel::NOTICE, $message, $context);
80
    }
81
82
    /**
83
     * @inheritDoc
84
     */
85
    public function info($message, array $context = array())
86
    {
87
        $this->log(LogLevel::INFO, $message, $context);
88
    }
89
90
    /**
91
     * @inheritDoc
92
     */
93
    public function debug($message, array $context = array())
94
    {
95
        $this->log(LogLevel::DEBUG, $message, $context);
96
    }
97
98
    /**
99
     * @inheritDoc
100
     */
101
    public function log($level, $message, array $context = array())
102
    {
103
        if (!isset(self::PSR_TO_LOGRUS_LEVELS[$level])) {
104
            throw new InvalidArgumentException(sprintf("Invalid level '%s': only constants of '%s' are allowed", $level, LogLevel::class));
105
        }
106
107
        $context['psr_level'] = $level;
108
109
        $this->rpc->call("log.Log", [
110
            "message" => $message,
111
            "level" => self::PSR_TO_LOGRUS_LEVELS[$level],
112
            "fields" => $context,
113
        ]);
114
    }
115
}