Logger   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 28
c 3
b 0
f 0
dl 0
loc 62
ccs 15
cts 20
cp 0.75
rs 10
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A debug() 0 3 1
A info() 0 3 1
A warn() 0 3 1
A fatal() 0 3 1
A error() 0 3 1
A log() 0 17 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Suricate;
6
7
/**
8
 * Logger extension for Suricate
9
 *
10
 * @package Suricate\Cache
11
 * @author  Mathieu LESNIAK <[email protected]>
12
 *
13
 * @property string $logfile
14
 * @property boolean $enabled
15
 * @property int $level
16
 * @property boolean $timestamp
17
 */
18
class Logger extends Service
19
{
20
    const LOGLEVEL_FATAL = 0;
21
    const LOGLEVEL_ERROR = 1;
22
    const LOGLEVEL_WARN = 2;
23
    const LOGLEVEL_INFO = 3;
24
    const LOGLEVEL_DEBUG = 4;
25
26
    protected $parametersList = ['logfile', 'enabled', 'level', 'timestamp'];
27
28
    private $resource;
29
30
    protected $levels = [
31
        self::LOGLEVEL_FATAL => 'FATAL',
32
        self::LOGLEVEL_ERROR => 'ERROR',
33
        self::LOGLEVEL_WARN => 'WARN',
34
        self::LOGLEVEL_INFO => 'INFO',
35
        self::LOGLEVEL_DEBUG => 'DEBUG'
36
    ];
37
38 2
    public function log($message, $level)
39
    {
40 2
        if ($this->resource == null && $this->logfile !== null) {
41 1
            $this->resource = fopen($this->logfile, 'a+');
42
        }
43
44 2
        if ($level <= $this->level && $this->enabled) {
45
            if ($this->timestamp) {
46
                $message = "[" . date('M d H:i:s') . "] " . $message;
47
            }
48
            fputs(
49
                $this->resource,
50
                '[' . $this->levels[$level] . '] ' . (string) $message . PHP_EOL
51
            );
52
        }
53
54 2
        return $this;
55
    }
56
57 1
    public function fatal($message)
58
    {
59 1
        return $this->log($message, self::LOGLEVEL_FATAL);
60
    }
61
62 1
    public function error($message)
63
    {
64 1
        return $this->log($message, self::LOGLEVEL_ERROR);
65
    }
66
67 1
    public function warn($message)
68
    {
69 1
        return $this->log($message, self::LOGLEVEL_WARN);
70
    }
71
72 1
    public function info($message)
73
    {
74 1
        return $this->log($message, self::LOGLEVEL_INFO);
75
    }
76
77 3
    public function debug($message)
78
    {
79 3
        return $this->log($message, self::LOGLEVEL_DEBUG);
80
    }
81
}
82