1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Retour plugin for Craft CMS 3.x |
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\FileTarget; |
17
|
|
|
|
18
|
|
|
/** |
|
|
|
|
19
|
|
|
* @author nystudio107 |
|
|
|
|
20
|
|
|
* @package Retour |
|
|
|
|
21
|
|
|
* @since 3.2.0 |
|
|
|
|
22
|
|
|
*/ |
|
|
|
|
23
|
|
|
class FileLog |
24
|
|
|
{ |
25
|
|
|
// Public Static Methods |
26
|
|
|
// ========================================================================= |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Create an additional file log target named $filename.log that logs messages |
30
|
|
|
* in the category $category |
31
|
|
|
* |
32
|
|
|
* @param string $fileName |
|
|
|
|
33
|
|
|
* @param string $category |
|
|
|
|
34
|
|
|
* @return void |
|
|
|
|
35
|
|
|
*/ |
36
|
|
|
public static function create(string $fileName, string $category): void |
37
|
|
|
{ |
38
|
|
|
// Create a new file target |
39
|
|
|
$fileTarget = new FileTarget([ |
|
|
|
|
40
|
|
|
'categories' => [$category], |
41
|
|
|
'levels' => ['error', 'warning', 'info'], |
42
|
|
|
'logFile' => "@storage/logs/$fileName.log", |
43
|
|
|
'logVars' => [], |
44
|
|
|
]); |
|
|
|
|
45
|
|
|
// Add the new target file target to the dispatcher |
46
|
|
|
Craft::getLogger()->dispatcher->targets[] = $fileTarget; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Delete the passed in log file $fileName |
51
|
|
|
* |
52
|
|
|
* @param string $fileName |
|
|
|
|
53
|
|
|
* @return void |
|
|
|
|
54
|
|
|
*/ |
55
|
|
|
public static function delete(string $fileName): void |
56
|
|
|
{ |
57
|
|
|
FileHelper::unlink(Craft::getAlias("@storage/logs/$fileName.log")); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get the contents of the log file |
62
|
|
|
* |
63
|
|
|
* @param string $fileName |
|
|
|
|
64
|
|
|
* @return string |
|
|
|
|
65
|
|
|
*/ |
66
|
|
|
public static function getContents(string $fileName): string |
67
|
|
|
{ |
68
|
|
|
$contents = @file_get_contents(Craft::getAlias("@storage/logs/$fileName.log")); |
|
|
|
|
69
|
|
|
|
70
|
|
|
return $contents === false ? '' : $contents; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|