Completed
Pull Request — master (#7)
by lee
23:32 queued 10s
created

Logger   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 50
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setLogFile() 0 4 1
A log() 0 5 1
A getLogMsg() 0 12 2
1
<?php
2
3
/**
4
 * @maintainer Timur Shagiakhmetov <[email protected]>
5
 */
6
7
namespace Badoo\LiveProfiler;
8
9
use Psr\Log\LoggerInterface;
10
use Psr\Log\LoggerTrait;
11
12
class Logger implements LoggerInterface
13
{
14
    use LoggerTrait;
15
16
    /** @var string */
17
    protected $logfile;
18
19
    /**
20
     * Logger constructor.
21
     */
22 19
    public function __construct()
23
    {
24 19
        $this->logfile = __DIR__ . '/../../../live.profiler.log';
25 19
    }
26
27 1
    public function setLogFile($logfile)
28
    {
29 1
        $this->logfile = $logfile;
30 1
    }
31
32
    /**
33
     * @param mixed $level
34
     * @param string $message
35
     * @param array $context
36
     */
37 1
    public function log($level, $message, array $context = array())
38
    {
39 1
        $log_string = $this->getLogMsg($level, $message, $context);
40 1
        file_put_contents($this->logfile, $log_string, FILE_APPEND);
41 1
    }
42
43
    /**
44
     * @param string $level
45
     * @param string $message
46
     * @param array $context
47
     * @return string
48
     */
49 2
    protected function getLogMsg($level, $message, array $context = array())
50
    {
51 2
        $log_string = sprintf("%s\t%s\t%s", date('Y-m-d H:i:s'), $level, $message);
52
53 2
        if (!empty($context)) {
54 1
            $log_string .= "\t" . json_encode($context, true);
55
        }
56
57 2
        $log_string .= "\n";
58
59 2
        return $log_string;
60
    }
61
}
62