ArrayUtil.php
1 year ago
DiscountsUtil.php
2 years ago
FeaturesUtil.php
10 months ago
I18nUtil.php
3 years ago
LoggingUtil.php
1 year ago
NumberUtil.php
10 months ago
OrderUtil.php
8 months ago
PluginUtil.php
7 months ago
RestApiUtil.php
2 years ago
ShippingUtil.php
1 year ago
StringUtil.php
2 years ago
TimeUtil.php
2 years ago
LoggingUtil.php
106 lines
| 1 | <?php |
| 2 | declare( strict_types=1 ); |
| 3 | |
| 4 | namespace Automattic\WooCommerce\Utilities; |
| 5 | |
| 6 | use Automattic\WooCommerce\Internal\Admin\Logging\{ PageController, Settings }; |
| 7 | use Automattic\WooCommerce\Internal\Admin\Logging\FileV2\{ File, FileController }; |
| 8 | |
| 9 | /** |
| 10 | * A class of utilities for dealing with logging. |
| 11 | */ |
| 12 | final class LoggingUtil { |
| 13 | /** |
| 14 | * Get the canonical URL for the Logs tab of the Status admin page. |
| 15 | * |
| 16 | * @return string |
| 17 | */ |
| 18 | public static function get_logs_tab_url(): string { |
| 19 | return wc_get_container()->get( PageController::class )->get_logs_tab_url(); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Determine the current value of the logging_enabled setting. |
| 24 | * |
| 25 | * @return bool |
| 26 | */ |
| 27 | public static function logging_is_enabled(): bool { |
| 28 | return wc_get_container()->get( Settings::class )->logging_is_enabled(); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Determine the current value of the default_handler setting. |
| 33 | * |
| 34 | * @return string |
| 35 | */ |
| 36 | public static function get_default_handler(): string { |
| 37 | return wc_get_container()->get( Settings::class )->get_default_handler(); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Determine the current value of the retention_period_days setting. |
| 42 | * |
| 43 | * @return int |
| 44 | */ |
| 45 | public static function get_retention_period(): int { |
| 46 | return wc_get_container()->get( Settings::class )->get_retention_period(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Determine the current value of the level_threshold setting. |
| 51 | * |
| 52 | * @return string |
| 53 | */ |
| 54 | public static function get_level_threshold(): string { |
| 55 | return wc_get_container()->get( Settings::class )->get_level_threshold(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Generate a public ID for a log file based on its properties. |
| 60 | * |
| 61 | * The file ID is the basename of the file without the hash part. It allows us to identify a file without revealing |
| 62 | * its full name in the filesystem, so that it's difficult to access the file directly with an HTTP request. |
| 63 | * |
| 64 | * @param string $source The source of the log entries contained in the file. |
| 65 | * @param int|null $rotation Optional. The 0-based incremental rotation marker, if the file has been rotated. |
| 66 | * Should only be a single digit. |
| 67 | * @param int $created Optional. The date the file was created, as a Unix timestamp. |
| 68 | * |
| 69 | * @return string |
| 70 | */ |
| 71 | public static function generate_log_file_id( string $source, ?int $rotation = null, int $created = 0 ): string { |
| 72 | return File::generate_file_id( $source, $rotation, $created ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Generate a hash to use as the suffix on a log filename. |
| 77 | * |
| 78 | * @param string $file_id A file ID (file basename without the hash). |
| 79 | * |
| 80 | * @return string |
| 81 | */ |
| 82 | public static function generate_log_file_hash( string $file_id ): string { |
| 83 | return File::generate_hash( $file_id ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Get the directory for storing log files. |
| 88 | * |
| 89 | * @param bool $create_dir Optional. True to attempt to create the log directory if it doesn't exist. Default true. |
| 90 | * |
| 91 | * @return string The full directory path, with trailing slash. |
| 92 | */ |
| 93 | public static function get_log_directory( bool $create_dir = true ): string { |
| 94 | return Settings::get_log_directory( $create_dir ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Calculate the size, in bytes, of the log directory. |
| 99 | * |
| 100 | * @return int |
| 101 | */ |
| 102 | public static function get_log_directory_size(): int { |
| 103 | return wc_get_container()->get( FileController::class )->get_log_directory_size(); |
| 104 | } |
| 105 | } |
| 106 |