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