Logger::fatal()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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