Log::warning()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php namespace GameScan\Core\Tools;
2
3
use Monolog\Logger;
4
use Monolog\Handler\StreamHandler;
5
use Monolog\Formatter\LineFormatter;
6
7
class Log
8
{
9
    const LOG_NAME = "GAME-SCAN";
10
    private $log = null;
11
    private $handlers = array();
12
    private $prefix = null;
13
    private $formatter = null;
14
    private $outputFormat = "[%datetime%] [%channel%] [%level_name%] %message% %context% %extra%\n";
15
    private $dateFormat =  "Y-m-d H:i:s";
16
    private $path =  "/tmp/gameScan.log";
17
    private $logLevel = 200;
18
    private $processor = null;
19
    private $availablesConfigs = [
20
        "outputFormat",
21
        "dateFormat",
22
        "path",
23
        "logLevel"
24
    ];
25
26
27 2
    public function __construct(\Monolog\Handler\AbstractHandler $handler = null, $formatter = null, $processor = null, array $config = array())
28
    {
29 2
        $this->loadConfig($config);
30 2
        $this->initializeFormatter($formatter);
31 2
        $this->initializeHandler($handler);
32 2
        $this->initializeProcessor($processor);
33 2
        $this->initializeLoger();
34 2
    }
35
36 2
    private function loadConfig($config)
37
    {
38 2
        foreach ($this->availablesConfigs as $configKey) {
39 2
            if (isset($config[$configKey])) {
40
                $this->$configKey = $config[$configKey];
41
            }
42 2
        }
43 2
    }
44
45 2
    private function initializeFormatter($formatter)
46
    {
47 2
        $this->formatter = $formatter === null ? new LineFormatter($this->outputFormat, $this->dateFormat) : $formatter ;
48 2
    }
49 2
    private function initializeHandler($handler)
50
    {
51 2
        $this->handlers[] = $handler === null ? new StreamHandler($this->path, $this->logLevel) : $handler ;
52 2
        $handler = end($this->handlers);
53 2
        $handler->setFormatter($this->formatter);
54 2
    }
55
56 2
    private function initializeProcessor($processor)
57
    {
58 2
        $this->processor = $processor === null ? new \Monolog\Processor\WebProcessor() : $processor ;
59 2
    }
60
61 2
    private function initializeLoger()
62
    {
63 2
        $this->log = new Logger(static::LOG_NAME);
64 2
        $this->log->pushHandler(end($this->handlers));
65 2
        $this->log->pushProcessor($this->processor);
66 2
    }
67
68
    public function addHandler(\Monolog\Handler\AbstractHandler $handler)
69
    {
70
        $this->initializeHandler($handler);
71
        $this->log->pushHandler($handler);
72
    }
73
74
75
    public function setHandler(\Monolog\Handler\AbstractHandler $handler)
76
    {
77
        $this->resetHandlers();
78
        $this->addHandler($handler);
79
    }
80
    private function resetHandlers()
81
    {
82
        array_map(
83
            function () {
84
                return $this->log->popHandler();
85
            },
86
            $this->log->getHandlers()
87
        );
88
89
        $this->handlers = array();
90
    }
91
92
93
    public function info($message)
94
    {
95
        $this->log->addInfo(
96
            $this->formatMessage($message)
97
        );
98
    }
99
100
101
    public function debug($message)
102
    {
103
        $this->log->addDebug(
104
            $this->formatMessage($message)
105
        );
106
    }
107
108
109
    public function warning($message)
110
    {
111
        $this->log->addWarning(
112
            $this->formatMessage($message)
113
        );
114
    }
115
116
117 2
    public function error($message)
118
    {
119 2
        $this->log->addError(
120 2
            $this->formatMessage($message)
121 2
        );
122 2
    }
123
124
125
    public function critical($message)
126
    {
127
        $this->log->addCritical(
128
            $this->formatMessage($message)
129
        );
130
    }
131
132
133
    public function emergency($message)
134
    {
135
        $this->log->addEmergency(
136
            $this->formatMessage($message)
137
        );
138
    }
139
    
140 2
    private function formatMessage($message)
141
    {
142 2
        return  $this->prefix . $message;
143
    }
144
145
    public function setPrefix($prefix)
146
    {
147
        $this->prefix = $prefix;
148
    }
149
}
150