summaryrefslogtreecommitdiffstats
path: root/vendor/textalk/websocket/tests/mock/EchoLog.php
blob: 369131a4003c45555561b555d18bc493c89aa6b7 (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
<?php

/**
 * Simple echo logger (only available when running in dev environment)
 */

namespace WebSocket;

class EchoLog implements \Psr\Log\LoggerInterface
{
    use \Psr\Log\LoggerTrait;

    public function log($level, $message, array $context = [])
    {
        $message = $this->interpolate($message, $context);
        $context_string = empty($context) ? '' : json_encode($context);
        echo str_pad($level, 8) . " | {$message} {$context_string}\n";
    }

    public function interpolate($message, array $context = [])
    {
        // Build a replacement array with braces around the context keys
        $replace = [];
        foreach ($context as $key => $val) {
            // Check that the value can be cast to string
            if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
                $replace['{' . $key . '}'] = $val;
            }
        }

        // Interpolate replacement values into the message and return
        return strtr($message, $replace);
    }
}