nystudio107 /
craft-retour
| 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/ |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 9 | * @copyright Copyright (c) 2022 nystudio107 |
||
|
0 ignored issues
–
show
|
|||
| 10 | */ |
||
|
0 ignored issues
–
show
|
|||
| 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 | /** |
||
|
0 ignored issues
–
show
|
|||
| 21 | * @author nystudio107 |
||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||
| 22 | * @package Retour |
||
|
0 ignored issues
–
show
|
|||
| 23 | * @since 4.1.0 |
||
|
0 ignored issues
–
show
|
|||
| 24 | */ |
||
|
0 ignored issues
–
show
|
|||
| 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 |
||
|
0 ignored issues
–
show
|
|||
| 40 | * @param string $category |
||
|
0 ignored issues
–
show
|
|||
| 41 | * @return void |
||
|
0 ignored issues
–
show
|
|||
| 42 | */ |
||
| 43 | public static function create(string $fileName, string $category): void |
||
| 44 | { |
||
| 45 | // Create a new file target |
||
| 46 | $fileTarget = new MonologTarget([ |
||
|
0 ignored issues
–
show
|
|||
| 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 | ]); |
||
|
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
Loading history...
|
|||
| 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 |
||
|
0 ignored issues
–
show
|
|||
| 67 | * @return void |
||
|
0 ignored issues
–
show
|
|||
| 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 |
||
|
0 ignored issues
–
show
|
|||
| 78 | * @return string |
||
|
0 ignored issues
–
show
|
|||
| 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 |
||
|
0 ignored issues
–
show
|
|||
| 91 | * @return string |
||
|
0 ignored issues
–
show
|
|||
| 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"); |
||
|
0 ignored issues
–
show
|
|||
| 98 | } |
||
| 99 | } |
||
| 100 |