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