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\helpers; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\log\FileTarget; |
13
|
|
|
use yii\log\Logger; |
14
|
|
|
use yii\web\Request; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Flipbox Factory <[email protected]> |
18
|
|
|
* @since 2.0.0 |
19
|
|
|
*/ |
20
|
|
|
class LoggerHelper |
21
|
|
|
{ |
22
|
|
|
public static $requireSession = true; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Takes an array of log categories and creates log target configs |
26
|
|
|
* |
27
|
|
|
* @param array $categories |
28
|
|
|
* @param array $targetConfig |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
|
|
public static function targetConfigs(array $categories, array $targetConfig = []): array |
32
|
|
|
{ |
33
|
|
|
$configs = []; |
34
|
|
|
|
35
|
|
|
foreach ($categories as $category) { |
36
|
|
|
$configs[$category] = static::targetConfig($category, $targetConfig); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return array_filter($configs); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Takes a log category and creates a log target config |
44
|
|
|
* |
45
|
|
|
* @param string $category |
46
|
|
|
* @param array $targetConfig |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public static function targetConfig(string $category, array $targetConfig = []): array |
50
|
|
|
{ |
51
|
|
|
// When empty, assume file target |
52
|
|
|
if (empty($targetConfig) || !isset($targetConfig['class'])) { |
53
|
|
|
return static::fileTargetConfig($category, $targetConfig); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return static::bootstrapConfig($category, $targetConfig); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Takes a log category and creates a log target config |
61
|
|
|
* |
62
|
|
|
* @param string $category |
63
|
|
|
* @param array $targetConfig |
64
|
|
|
* @return array |
65
|
|
|
* @since 2.3.3 |
66
|
|
|
*/ |
67
|
|
|
public static function fileTargetConfig(string $category, array $targetConfig = []): array |
68
|
|
|
{ |
69
|
|
|
$generalConfig = Craft::$app->getConfig()->getGeneral(); |
70
|
|
|
|
71
|
|
|
return static::targetConfig( |
72
|
|
|
$category, |
73
|
|
|
array_merge( |
74
|
|
|
[ |
75
|
|
|
'class' => FileTarget::class, |
76
|
|
|
'logFile' => '@storage/logs/' . $category . '.log', |
77
|
|
|
'fileMode' => $generalConfig->defaultFileMode, |
78
|
|
|
'dirMode' => $generalConfig->defaultDirMode |
79
|
|
|
], |
80
|
|
|
$targetConfig |
81
|
|
|
) |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $category |
87
|
|
|
* @param array $config |
88
|
|
|
* @return array |
89
|
|
|
* @since 2.6.2 |
90
|
|
|
*/ |
91
|
|
|
public static function bootstrapConfig(string $category, array $config = []): array |
92
|
|
|
{ |
93
|
|
|
$request = Craft::$app->getRequest(); |
94
|
|
|
// Only log console requests and web requests that aren't getAuthTimeout requests |
95
|
|
|
$isConsoleRequest = $request instanceof Request && $request->getIsConsoleRequest(); |
96
|
|
|
if (!$isConsoleRequest && (static::$requireSession && !Craft::$app->getUser()->enableSession)) { |
97
|
|
|
return []; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$target = [ |
101
|
|
|
'logVars' => [], |
102
|
|
|
'categories' => [$category, $category . ':*'] |
103
|
|
|
]; |
104
|
|
|
|
105
|
|
|
if (!$isConsoleRequest) { |
106
|
|
|
// Only log errors and warnings, unless Craft is running in Dev Mode or it's being installed/updated |
107
|
|
|
if (!YII_DEBUG |
108
|
|
|
&& Craft::$app->getIsInstalled() |
109
|
|
|
&& !Craft::$app->getUpdates()->getIsCraftDbMigrationNeeded() |
110
|
|
|
) { |
111
|
|
|
$target['levels'] = Logger::LEVEL_ERROR | Logger::LEVEL_WARNING; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return array_merge($target, $config); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return callable |
120
|
|
|
* |
121
|
|
|
* @deprecated |
122
|
|
|
*/ |
123
|
|
|
public static function config(): callable |
124
|
|
|
{ |
125
|
|
|
return function (string $category, array $config = []) { |
126
|
|
|
return static::bootstrapConfig($category, $config); |
127
|
|
|
}; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|