1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SixtyNine\Cloud\Factory; |
4
|
|
|
|
5
|
|
|
use Monolog\Handler\ErrorLogHandler; |
6
|
|
|
use Monolog\Handler\NullHandler; |
7
|
|
|
use Monolog\Handler\StreamHandler; |
8
|
|
|
use Monolog\Logger as MonologLogger; |
9
|
|
|
|
10
|
|
|
class Logger |
11
|
|
|
{ |
12
|
|
|
const DEBUG = 100; |
13
|
|
|
const INFO = 200; |
14
|
|
|
const NOTICE = 250; |
15
|
|
|
const WARNING = 300; |
16
|
|
|
const ERROR = 400; |
17
|
|
|
const CRITICAL = 500; |
18
|
|
|
const ALERT = 550; |
19
|
|
|
const EMERGENCY = 600; |
20
|
|
|
|
21
|
|
|
/** @var Logger */ |
22
|
|
|
protected static $instance; |
23
|
|
|
/** @var Logger */ |
24
|
|
|
protected $logger; |
25
|
|
|
/** @var string */ |
26
|
|
|
protected $filename; |
27
|
|
|
/** @var bool */ |
28
|
|
|
protected $outputChosen = false; |
29
|
|
|
|
30
|
1 |
|
protected function __construct() |
31
|
|
|
{ |
32
|
1 |
|
$this->logger = new MonologLogger('log'); |
|
|
|
|
33
|
1 |
|
} |
34
|
|
|
|
35
|
5 |
|
public static function getInstance() |
36
|
|
|
{ |
37
|
5 |
|
if (!self::$instance) { |
38
|
1 |
|
self::$instance = new self(); |
39
|
|
|
} |
40
|
5 |
|
return self::$instance; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function toConsole() |
44
|
|
|
{ |
45
|
|
|
$this->outputChosen = true; |
46
|
|
|
$this->logger->pushHandler(new ErrorLogHandler()); |
|
|
|
|
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function toFile($filename, $level = MonologLogger::DEBUG) |
51
|
|
|
{ |
52
|
|
|
if ($this->filename) { |
53
|
|
|
throw new \InvalidArgumentException('Log file already set'); |
54
|
|
|
} |
55
|
|
|
$this->outputChosen = true; |
56
|
|
|
$this->logger->pushHandler(new StreamHandler($filename, $level)); |
|
|
|
|
57
|
|
|
$this->filename = $filename; |
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
5 |
|
public function log($message, $level = MonologLogger::INFO, array $context = array()) |
62
|
|
|
{ |
63
|
5 |
|
if (!$this->outputChosen) { |
64
|
1 |
|
$this->outputChosen = true; |
65
|
1 |
|
$this->logger->pushHandler(new NullHandler()); |
|
|
|
|
66
|
|
|
} |
67
|
5 |
|
$this->logger->log($level, $message, $context); |
68
|
5 |
|
} |
69
|
|
|
|
70
|
|
|
public function getLogger() |
71
|
|
|
{ |
72
|
|
|
return $this->logger; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..