|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE |
|
6
|
|
|
* @link https://github.com/flipboxfactory/craft-ember/ |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\craft\ember\modules; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use yii\log\Logger; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* This trait will prefix log items to a specific category. |
|
16
|
|
|
* |
|
17
|
|
|
* To properly implement, add a static $category attribute to your module such as: |
|
18
|
|
|
* |
|
19
|
|
|
* ```public static $category = 'some-category';``` |
|
20
|
|
|
* |
|
21
|
|
|
* Then log a message through the module using the static log methods found within this trait. |
|
22
|
|
|
* |
|
23
|
|
|
* Additionally, you can log to a separate log file by appending log targets via the `config/app.php` file |
|
24
|
|
|
* |
|
25
|
|
|
* ```php |
|
26
|
|
|
* [ |
|
27
|
|
|
* 'components' => [ |
|
28
|
|
|
* 'log' => function() { |
|
29
|
|
|
* $config = craft\helpers\App::logConfig(); |
|
30
|
|
|
* |
|
31
|
|
|
* $targetConfigs = \flipbox\craft\ember\helpers\LoggerHelper::targetConfigs( |
|
32
|
|
|
* ['some-category', 'some-other-category'] |
|
33
|
|
|
* ); |
|
34
|
|
|
* |
|
35
|
|
|
* foreach ($targetConfigs as $key => $targetConfig) { |
|
36
|
|
|
* $config['targets'][$key] = $targetConfig; |
|
37
|
|
|
* } |
|
38
|
|
|
* |
|
39
|
|
|
return $config ? Craft::createObject($config) : null; |
|
40
|
|
|
* } |
|
41
|
|
|
* ] |
|
42
|
|
|
* ] |
|
43
|
|
|
* ``` |
|
44
|
|
|
* |
|
45
|
|
|
* @author Flipbox Factory <[email protected]> |
|
46
|
|
|
* @since 2.0.0 |
|
47
|
|
|
*/ |
|
48
|
|
|
trait LoggerTrait |
|
49
|
|
|
{ |
|
50
|
|
|
/** |
|
51
|
|
|
* @return Logger |
|
52
|
|
|
* |
|
53
|
|
|
* @deprecated |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function getLogger() |
|
56
|
|
|
{ |
|
57
|
|
|
return Craft::getLogger(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* The log categories |
|
62
|
|
|
* |
|
63
|
|
|
* @param $category |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
protected static function loggerCategory($category): string |
|
67
|
|
|
{ |
|
68
|
|
|
/** @noinspection PhpUndefinedFieldInspection */ |
|
69
|
|
|
$prefix = static::$category ?? ''; |
|
70
|
|
|
|
|
71
|
|
|
if (empty($category)) { |
|
72
|
|
|
return $prefix; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return ($prefix ? $prefix . ':' : '') . $category; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Logs a debug message. |
|
80
|
|
|
* Trace messages are logged mainly for development purpose to see |
|
81
|
|
|
* the execution work flow of some code. This method will only log |
|
82
|
|
|
* a message when the application is in debug mode. |
|
83
|
|
|
* @param string|array $message the message to be logged. This can be a simple string or a more |
|
84
|
|
|
* complex data structure, such as array. |
|
85
|
|
|
* @param string $category the category of the message. |
|
86
|
|
|
* @since 2.0.14 |
|
87
|
|
|
*/ |
|
88
|
|
|
public static function debug($message, $category = 'general') |
|
89
|
|
|
{ |
|
90
|
|
|
Craft::getLogger()->log($message, Logger::LEVEL_TRACE, static::loggerCategory($category)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Logs an error message. |
|
95
|
|
|
* An error message is typically logged when an unrecoverable error occurs |
|
96
|
|
|
* during the execution of an application. |
|
97
|
|
|
* @param string|array $message the message to be logged. This can be a simple string or a more |
|
98
|
|
|
* complex data structure, such as array. |
|
99
|
|
|
* @param string $category the category of the message. |
|
100
|
|
|
*/ |
|
101
|
|
|
public static function error($message, $category = 'general') |
|
102
|
|
|
{ |
|
103
|
|
|
Craft::getLogger()->log($message, Logger::LEVEL_ERROR, static::loggerCategory($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|array $message the message to be logged. This can be a simple string or a more |
|
111
|
|
|
* complex data structure, such as array. |
|
112
|
|
|
* @param string $category the category of the message. |
|
113
|
|
|
*/ |
|
114
|
|
|
public static function warning($message, $category = 'general') |
|
115
|
|
|
{ |
|
116
|
|
|
Craft::getLogger()->log($message, Logger::LEVEL_WARNING, static::loggerCategory($category)); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Logs an informative message. |
|
121
|
|
|
* An informative message is typically logged by an application to keep record of |
|
122
|
|
|
* something important (e.g. an administrator logs in). |
|
123
|
|
|
* @param string|array $message the message to be logged. This can be a simple string or a more |
|
124
|
|
|
* complex data structure, such as array. |
|
125
|
|
|
* @param string $category the category of the message. |
|
126
|
|
|
*/ |
|
127
|
|
|
public static function info($message, $category = 'general') |
|
128
|
|
|
{ |
|
129
|
|
|
Craft::getLogger()->log($message, Logger::LEVEL_INFO, static::loggerCategory($category)); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|