Completed
Pull Request — master (#40)
by Patrick
05:27
created

LogService::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Log;
3
require('vendor/autoload.php');
4
5
abstract class LogService extends \Psr\Log\AbstractLogger
6
{
7
    protected $logLevels = array(
8
        \Psr\Log\LogLevel::EMERGENCY,
9
        \Psr\Log\LogLevel::ALERT,
10
        \Psr\Log\LogLevel::CRITICAL,
11
        \Psr\Log\LogLevel::ERROR,
12
        \Psr\Log\LogLevel::WARNING,
13
        \Psr\Log\LogLevel::NOTICE);
14
15
    public function __construct($params=false)
16
    {
17
        if(isset($params['defaultLogLevels']))
18
        {
19
            $this->logLevels = $params['defaultLogLevels'];
20
        }
21
    }
22
23
    /**
24
     * Interpolates context values into the message placeholders.
25
     */
26
    protected function interpolate($message, array $context = array())
27
    {
28
        // build a replacement array with braces around the context keys
29
        $replace = array();
30
        foreach ($context as $key => $val) {
31
            // check that the value can be casted to string
32
            if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
33
                $replace['{' . $key . '}'] = $val;
34
            }
35
        }
36
37
        // interpolate replacement values into the message and return
38
        return strtr($message, $replace);
39
    }
40
41
    protected function shouldLog($level)
42
    {
43
        return in_array($level, $this->logLevels);
44
    }
45
}
46
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
47