|
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
|
|
|
/** |
|
23
|
|
|
* Takes an array of log categories and creates log target configs |
|
24
|
|
|
* |
|
25
|
|
|
* @param array $categories |
|
26
|
|
|
* @return array |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function targetConfigs(array $categories): array |
|
29
|
|
|
{ |
|
30
|
|
|
$configs = []; |
|
31
|
|
|
|
|
32
|
|
|
foreach ($categories as $category) { |
|
33
|
|
|
$configs[$category] = static::targetConfig($category); |
|
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
|
|
|
* @return array |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function targetConfig(string $category): array |
|
46
|
|
|
{ |
|
47
|
|
|
// Only log console requests and web requests that aren't getAuthTimeout requests |
|
48
|
|
|
$isConsoleRequest = Craft::$app->getRequest()->getIsConsoleRequest(); |
|
49
|
|
|
if (!$isConsoleRequest && !Craft::$app->getUser()->enableSession) { |
|
50
|
|
|
return []; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$generalConfig = Craft::$app->getConfig()->getGeneral(); |
|
54
|
|
|
|
|
55
|
|
|
$target = [ |
|
56
|
|
|
'class' => FileTarget::class, |
|
57
|
|
|
'fileMode' => $generalConfig->defaultFileMode, |
|
58
|
|
|
'dirMode' => $generalConfig->defaultDirMode, |
|
59
|
|
|
'logVars' => [], |
|
60
|
|
|
'categories' => [$category, $category . ':*'], |
|
61
|
|
|
'logFile' => '@storage/logs/'.$category.'.log' |
|
62
|
|
|
]; |
|
63
|
|
|
|
|
64
|
|
|
if (!$isConsoleRequest) { |
|
65
|
|
|
// Only log errors and warnings, unless Craft is running in Dev Mode or it's being installed/updated |
|
66
|
|
|
if (!YII_DEBUG |
|
67
|
|
|
&& Craft::$app->getIsInstalled() |
|
68
|
|
|
&& !Craft::$app->getUpdates()->getIsCraftDbMigrationNeeded() |
|
69
|
|
|
) { |
|
70
|
|
|
$target['levels'] = Logger::LEVEL_ERROR | Logger::LEVEL_WARNING; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $target; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|