|
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\helpers; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use craft\log\FileTarget; |
|
13
|
|
|
use craft\web\Request; |
|
14
|
|
|
use flipbox\spark\Modules\interfaces\LoggableInterface; |
|
15
|
|
|
use yii\base\Module; |
|
16
|
|
|
use yii\log\Dispatcher; |
|
17
|
|
|
use yii\log\Logger; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @author Flipbox Factory <[email protected]> |
|
21
|
|
|
* @since 1.0.0 |
|
22
|
|
|
*/ |
|
23
|
|
|
class LoggingHelper |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* prefixSessionData. |
|
27
|
|
|
* |
|
28
|
|
|
* @return callable |
|
29
|
|
|
*/ |
|
30
|
|
|
public static function prefixSessionData() |
|
31
|
|
|
{ |
|
32
|
|
|
return function () { |
|
33
|
|
|
$request = Craft::$app->getRequest(); |
|
34
|
|
|
$ip = $request instanceof Request ? $request->getUserIP() : '-'; |
|
35
|
|
|
|
|
36
|
|
|
/* @var $user \craft\web\User */ |
|
37
|
|
|
$user = Craft::$app->has('user', true) ? Craft::$app->get('user') : null; |
|
38
|
|
|
if ($user && ($identity = $user->getIdentity())) { |
|
39
|
|
|
$userID = $identity->getId() . ':' . $identity->username; |
|
40
|
|
|
} else { |
|
41
|
|
|
$userID = '-'; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/* @var $session \yii\web\Session */ |
|
45
|
|
|
$session = Craft::$app->has('session', true) ? Craft::$app->get('session') : null; |
|
46
|
|
|
$sessionID = $session && $session->getIsActive() ? $session->getId() : '-'; |
|
47
|
|
|
|
|
48
|
|
|
return "[$ip][$userID][$sessionID]"; |
|
49
|
|
|
}; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* isDebugModeEnabled. |
|
54
|
|
|
* |
|
55
|
|
|
* @param Module $module |
|
56
|
|
|
* |
|
57
|
|
|
* @return bool |
|
58
|
|
|
*/ |
|
59
|
|
|
public static function isDebugModeEnabled(Module $module) |
|
60
|
|
|
{ |
|
61
|
|
|
return Craft::$app->getConfig()->getGeneral()->devMode || |
|
62
|
|
|
($module instanceof LoggableInterface && $module->isDebugModeEnabled()); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* getDispatchDefinition. |
|
67
|
|
|
* |
|
68
|
|
|
* @param Module $module |
|
69
|
|
|
* @param array $config |
|
70
|
|
|
* |
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
|
|
public static function getDispatchDefinition(Module $module, array $config = []) |
|
74
|
|
|
{ |
|
75
|
|
|
|
|
76
|
|
|
$configService = Craft::$app->getConfig()->getGeneral(); |
|
77
|
|
|
|
|
78
|
|
|
$defaultConfig = [ |
|
79
|
|
|
'logger' => |
|
80
|
|
|
/* '\yii\log\Logger', */ |
|
81
|
|
|
new Logger(), |
|
82
|
|
|
'class' => Dispatcher::class, |
|
83
|
|
|
'targets' => [ |
|
84
|
|
|
/* 'file' => $fileTarget, */ |
|
85
|
|
|
'file' => [ |
|
86
|
|
|
'class' => FileTarget::class, |
|
87
|
|
|
'levels' => array_merge( |
|
88
|
|
|
['error', 'warning'], |
|
89
|
|
|
static::isDebugModeEnabled($module) ? ['trace', 'info'] : [] |
|
90
|
|
|
), |
|
91
|
|
|
'logFile' => Craft::getAlias('@storage/logs/' . strtolower(str_replace( |
|
92
|
|
|
'/', |
|
93
|
|
|
'-', |
|
94
|
|
|
$module->getUniqueId() |
|
95
|
|
|
)) . '.log'), |
|
96
|
|
|
'logVars' => [], |
|
97
|
|
|
'fileMode' => $configService->defaultFileMode, |
|
98
|
|
|
'dirMode' => $configService->defaultDirMode, |
|
99
|
|
|
'prefix' => static::prefixSessionData(), |
|
100
|
|
|
], |
|
101
|
|
|
], |
|
102
|
|
|
]; |
|
103
|
|
|
|
|
104
|
|
|
return ArrayHelper::merge($config, $defaultConfig); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|