1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://github.com/flipbox/spark/blob/master/LICENSE |
6
|
|
|
* @link https://github.com/flipbox/spark |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\spark\modules\traits; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use flipbox\spark\helpers\LoggingHelper; |
13
|
|
|
use yii\log\Dispatcher; |
14
|
|
|
use yii\log\Logger; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Flipbox Factory <[email protected]> |
18
|
|
|
* @since 1.0.0 |
19
|
|
|
*/ |
20
|
|
|
trait LoggableTrait |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
private static $dispatcher; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @inheritdoc |
27
|
|
|
*/ |
28
|
|
|
public function isDebugModeEnabled() |
29
|
|
|
{ |
30
|
|
|
return false; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return Logger |
35
|
|
|
*/ |
36
|
|
|
public static function getLogger() |
37
|
|
|
{ |
38
|
|
|
return static::getDispatcher()->getLogger(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Sets the logger object. |
43
|
|
|
* @param Logger $logger the logger object. |
44
|
|
|
*/ |
45
|
|
|
public static function setLogger($logger) |
46
|
|
|
{ |
47
|
|
|
static::getDispatcher()->setLogger($logger); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return Dispatcher |
52
|
|
|
*/ |
53
|
|
|
public static function getDispatcher() |
54
|
|
|
{ |
55
|
|
|
if (self::$dispatcher !== null) { |
56
|
|
|
return self::$dispatcher; |
57
|
|
|
} else { |
58
|
|
|
static::setDispatcher(); |
59
|
|
|
return static::$dispatcher; |
|
|
|
|
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param array $dispatcher |
65
|
|
|
* @throws \yii\base\InvalidConfigException |
66
|
|
|
*/ |
67
|
|
|
public static function setDispatcher($dispatcher = []) |
68
|
|
|
{ |
69
|
|
|
|
70
|
|
|
if (!$dispatcher instanceof Dispatcher) { |
71
|
|
|
$dispatcher = Craft::createObject( |
72
|
|
|
LoggingHelper::getDispatchDefinition( |
73
|
|
|
static::getInstance(), |
74
|
|
|
$dispatcher |
75
|
|
|
) |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
self::$dispatcher = $dispatcher; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Logs a trace message. |
84
|
|
|
* Trace messages are logged mainly for development purpose to see |
85
|
|
|
* the execution work flow of some code. |
86
|
|
|
* @param string $message the message to be logged. |
87
|
|
|
* @param string $category the category of the message. |
88
|
|
|
*/ |
89
|
|
|
public static function trace($message, $category = 'module') |
90
|
|
|
{ |
91
|
|
|
static::getLogger()->log($message, Logger::LEVEL_TRACE, $category); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Logs an error message. |
96
|
|
|
* An error message is typically logged when an unrecoverable error occurs |
97
|
|
|
* during the execution of an application. |
98
|
|
|
* @param string $message the message to be logged. |
99
|
|
|
* @param string $category the category of the message. |
100
|
|
|
*/ |
101
|
|
|
public static function error($message, $category = 'module') |
102
|
|
|
{ |
103
|
|
|
static::getLogger()->log($message, Logger::LEVEL_ERROR, $category); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Logs a warning message. |
108
|
|
|
* A warning message is typically logged when an error occurs while the execution |
109
|
|
|
* can still continue. |
110
|
|
|
* @param string $message the message to be logged. |
111
|
|
|
* @param string $category the category of the message. |
112
|
|
|
*/ |
113
|
|
|
public static function warning($message, $category = 'module') |
114
|
|
|
{ |
115
|
|
|
static::getLogger()->log($message, Logger::LEVEL_WARNING, $category); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Logs an informative message. |
120
|
|
|
* An informative message is typically logged by an application to keep record of |
121
|
|
|
* something important (e.g. an administrator logs in). |
122
|
|
|
* @param string $message the message to be logged. |
123
|
|
|
* @param string $category the category of the message. |
124
|
|
|
*/ |
125
|
|
|
public static function info($message, $category = 'module') |
126
|
|
|
{ |
127
|
|
|
static::getLogger()->log($message, Logger::LEVEL_INFO, $category); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: