|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Retour plugin for Craft CMS |
|
4
|
|
|
* |
|
5
|
|
|
* Retour allows you to intelligently redirect legacy URLs, so that you don't |
|
6
|
|
|
* lose SEO value when rebuilding & restructuring a website |
|
7
|
|
|
* |
|
8
|
|
|
* @link https://nystudio107.com/ |
|
|
|
|
|
|
9
|
|
|
* @copyright Copyright (c) 2022 nystudio107 |
|
|
|
|
|
|
10
|
|
|
*/ |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
namespace nystudio107\retour\helpers; |
|
13
|
|
|
|
|
14
|
|
|
use Craft; |
|
15
|
|
|
use craft\helpers\FileHelper; |
|
16
|
|
|
use craft\log\MonologTarget; |
|
17
|
|
|
use Monolog\Formatter\LineFormatter; |
|
18
|
|
|
use Psr\Log\LogLevel; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
|
|
|
|
|
21
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
22
|
|
|
* @package Retour |
|
|
|
|
|
|
23
|
|
|
* @since 4.1.0 |
|
|
|
|
|
|
24
|
|
|
*/ |
|
|
|
|
|
|
25
|
|
|
class FileLog |
|
26
|
|
|
{ |
|
27
|
|
|
// Constants |
|
28
|
|
|
// ========================================================================= |
|
29
|
|
|
|
|
30
|
|
|
public const FILE_PER_DAY = 'Y-m-d'; |
|
31
|
|
|
|
|
32
|
|
|
// Public Static Methods |
|
33
|
|
|
// ========================================================================= |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Create an additional file log target named $filename.log that logs messages |
|
37
|
|
|
* in the category $category |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $fileName |
|
|
|
|
|
|
40
|
|
|
* @param string $category |
|
|
|
|
|
|
41
|
|
|
* @return void |
|
|
|
|
|
|
42
|
|
|
*/ |
|
43
|
|
|
public static function create(string $fileName, string $category): void |
|
44
|
|
|
{ |
|
45
|
|
|
// Create a new file target |
|
46
|
|
|
$fileTarget = new MonologTarget([ |
|
|
|
|
|
|
47
|
|
|
'name' => $fileName, |
|
48
|
|
|
'categories' => [$category], |
|
49
|
|
|
'level' => LogLevel::INFO, |
|
50
|
|
|
'logContext' => false, |
|
51
|
|
|
'allowLineBreaks' => true, |
|
52
|
|
|
'formatter' => new LineFormatter( |
|
53
|
|
|
format: "%datetime% [%channel%.%level_name%] [%extra.yii_category%] %message% %context% %extra%\n", |
|
54
|
|
|
dateFormat: 'Y-m-d H:i:s', |
|
55
|
|
|
allowInlineLineBreaks: true, |
|
56
|
|
|
ignoreEmptyContextAndExtra: true, |
|
57
|
|
|
), |
|
58
|
|
|
]); |
|
|
|
|
|
|
59
|
|
|
// Add the new target file target to the dispatcher |
|
60
|
|
|
Craft::getLogger()->dispatcher->targets[] = $fileTarget; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Delete the passed in log file $fileName |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $fileName |
|
|
|
|
|
|
67
|
|
|
* @return void |
|
|
|
|
|
|
68
|
|
|
*/ |
|
69
|
|
|
public static function delete(string $fileName): void |
|
70
|
|
|
{ |
|
71
|
|
|
FileHelper::unlink(self::getLogFilePath($fileName)); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get the contents of the log file |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $fileName |
|
|
|
|
|
|
78
|
|
|
* @return string |
|
|
|
|
|
|
79
|
|
|
*/ |
|
80
|
|
|
public static function getContents(string $fileName): string |
|
81
|
|
|
{ |
|
82
|
|
|
$contents = @file_get_contents(self::getLogFilePath($fileName)); |
|
83
|
|
|
|
|
84
|
|
|
return $contents === false ? '' : $contents; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Return a full path to the log file |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $fileName |
|
|
|
|
|
|
91
|
|
|
* @return string |
|
|
|
|
|
|
92
|
|
|
*/ |
|
93
|
|
|
public static function getLogFilePath(string $fileName): string |
|
94
|
|
|
{ |
|
95
|
|
|
$logfile = $fileName . "-" . date(self::FILE_PER_DAY); |
|
96
|
|
|
|
|
97
|
|
|
return Craft::getAlias("@storage/logs/$logfile.log"); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|