1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Adam Jakab. |
4
|
|
|
* Date: 08/03/16 |
5
|
|
|
* Time: 12.37 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace SuiteCrm\Install; |
9
|
|
|
|
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Custom implementation of include/SugarLogger/LoggerManager.php |
14
|
|
|
* |
15
|
|
|
* Class LoggerManager |
16
|
|
|
* @package SuiteCrm\Install |
17
|
|
|
*/ |
18
|
|
|
class LoggerManager { |
19
|
|
|
/** @var OutputInterface */ |
20
|
|
|
protected $cmdOutput; |
21
|
|
|
|
22
|
|
|
/** @var bool */ |
23
|
|
|
protected $logToConsole = TRUE; |
24
|
|
|
|
25
|
|
|
/** @var string */ |
26
|
|
|
protected $defaultLogLevel = 'debug'; |
27
|
|
|
|
28
|
|
|
/** @var array */ |
29
|
|
|
private static $logLevelMapping = [ |
30
|
|
|
'debug' => 100, |
31
|
|
|
'info' => 70, |
32
|
|
|
'warn' => 50, |
33
|
|
|
'deprecated' => 40, |
34
|
|
|
'error' => 25, |
35
|
|
|
'fatal' => 10, |
36
|
|
|
'security' => 5, |
37
|
|
|
'off' => 0, |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param OutputInterface $cmdOutput |
42
|
|
|
* @param string $defaultLogLevel |
43
|
|
|
*/ |
44
|
|
|
public function __construct(OutputInterface $cmdOutput, $defaultLogLevel = 'debug') { |
45
|
|
|
$this->cmdOutput = $cmdOutput; |
46
|
|
|
$this->setLevel($defaultLogLevel); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $logLevel |
51
|
|
|
* @param mixed $message |
52
|
|
|
*/ |
53
|
|
|
public function __call($logLevel, $message) { |
54
|
|
|
$logLevel = (!in_array($logLevel, array_keys(self::$logLevelMapping)) ? $this->defaultLogLevel : $logLevel); |
55
|
|
|
if ($this->wouldLog($logLevel)) { |
56
|
|
|
$this->log($message, $logLevel); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $msg |
62
|
|
|
* @param string $level |
63
|
|
|
*/ |
64
|
|
|
protected function log($msg, $level) { |
65
|
|
|
if ($this->logToConsole) { |
66
|
|
|
$msg = is_array($msg) ? implode(" - ", $msg) : $msg; |
67
|
|
|
$this->cmdOutput->writeln("[$level]: " . $msg); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $logLevel |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
public function wouldLog($logLevel) { |
76
|
|
|
$logLevel = (!in_array($logLevel, array_keys(self::$logLevelMapping)) ? $this->defaultLogLevel : $logLevel); |
77
|
|
|
$wouldLog = $logLevel == $this->defaultLogLevel |
78
|
|
|
|| self::$logLevelMapping[$this->defaultLogLevel] >= self::$logLevelMapping[$logLevel]; |
79
|
|
|
return $wouldLog; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $message |
84
|
|
|
* @param boolean $condition |
85
|
|
|
*/ |
86
|
|
|
public function assert($message, $condition) { |
87
|
|
|
//do nothing |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $logLevel |
92
|
|
|
*/ |
93
|
|
|
public function setLevel($logLevel) { |
94
|
|
|
if(in_array($logLevel, array_keys(self::$logLevelMapping))) { |
95
|
|
|
$this->defaultLogLevel = $logLevel; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @throws \Exception |
101
|
|
|
*/ |
102
|
|
|
public static function getLogger() { |
103
|
|
|
throw new \Exception("getLogger is not available now!"); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $level |
108
|
|
|
* @param string $logger |
109
|
|
|
*/ |
110
|
|
|
public static function setLogger($level, $logger) { |
111
|
|
|
//do nothing |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
public static function getAvailableLoggers() { |
118
|
|
|
return []; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
public static function getLoggerLevels() { |
125
|
|
|
return self::$logLevelMapping; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |