CLI
1 year ago
Core
9 months ago
DemoSites
1 year ago
AssetsDependencyInjector.php
1 year ago
Config.php
1 year ago
FileLog.php
1 year ago
Flags.php
1 year ago
GoogleFontsLocalLoader.php
1 year ago
GutenbergControls.php
1 year ago
Migrations.php
1 year ago
NotificationsManager.php
1 year ago
PluginsManager.php
2 years ago
FileLog.php
185 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio; |
| 4 | |
| 5 | /** |
| 6 | * @method static info( string $location, string|array $message) |
| 7 | * @method static error( string $location, string|array $message) |
| 8 | * @method static warning( string $location, string|array $message) |
| 9 | * @method static FileLog with_type( string $type ) |
| 10 | * @method info( string $location, string|array $message) |
| 11 | * @method error( string $location, string|array $message) |
| 12 | * @method warning( string $location, string|array $message) |
| 13 | * @method FileLog with_type( string $type ) |
| 14 | */ |
| 15 | class FileLog { |
| 16 | |
| 17 | const CLASSIC_LOG = 'classic'; |
| 18 | const JSONL_LOG = 'jsonl'; |
| 19 | |
| 20 | private $log_type = 'classic'; |
| 21 | |
| 22 | |
| 23 | |
| 24 | private static function get_logs_root_path( $location ) { |
| 25 | $upload_dir = wp_upload_dir(); |
| 26 | $base_dir = untrailingslashit( $upload_dir['basedir'] ) . "/kubio-log/{$location}"; |
| 27 | |
| 28 | return $base_dir; |
| 29 | } |
| 30 | |
| 31 | private function prepare_file( $location, $type ) { |
| 32 | $type = strtolower( $type ); |
| 33 | |
| 34 | $base_dir = static::get_logs_root_path( $location ); |
| 35 | |
| 36 | if ( ! file_exists( $base_dir ) ) { |
| 37 | $result = wp_mkdir_p( $base_dir ); |
| 38 | |
| 39 | if ( ! $result ) { |
| 40 | return false; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | $date = gmdate( 'Y-m-d' ); |
| 45 | $extension = 'log'; |
| 46 | |
| 47 | switch ( $this->log_type ) { |
| 48 | case static::JSONL_LOG: |
| 49 | $extension = 'jsonl'; |
| 50 | break; |
| 51 | } |
| 52 | |
| 53 | $file_path = "{$base_dir}/{$type}-{$date}.{$extension}"; |
| 54 | |
| 55 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_is_writable |
| 56 | if ( file_exists( $file_path ) && ! is_writable( $file_path ) ) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | return $file_path; |
| 61 | } |
| 62 | |
| 63 | private function classic_log( $location, $message, $type = 'info' ) { |
| 64 | |
| 65 | $file_path = $this->prepare_file( $location, $type ); |
| 66 | |
| 67 | if ( ! $file_path ) { |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | $response = file_put_contents( |
| 72 | $file_path, |
| 73 | sprintf( |
| 74 | "LOG: %s ---------- %s ----------\n\n\n%s\n\n", |
| 75 | str_pad( $type, 6 ), |
| 76 | gmdate( 'Y-m-d H:i:s' ), |
| 77 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export |
| 78 | is_string( $message ) ? trim( $message ) : var_export( $message, true ) |
| 79 | ), |
| 80 | FILE_APPEND |
| 81 | ); |
| 82 | |
| 83 | if ( $response === false ) { |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | private function jsonl_log( $location, $message, $type = 'info' ) { |
| 91 | $file_path = $this->prepare_file( $location, $type ); |
| 92 | |
| 93 | if ( ! $file_path ) { |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | $content = array( |
| 98 | 'type' => $type, |
| 99 | 'date' => time(), |
| 100 | 'data' => $message, |
| 101 | ); |
| 102 | |
| 103 | $response = file_put_contents( |
| 104 | $file_path, |
| 105 | json_encode( $content ) . "\n", |
| 106 | FILE_APPEND |
| 107 | ); |
| 108 | |
| 109 | if ( $response === false ) { |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | private function store_log( $location, $message, $type = 'info' ) { |
| 117 | |
| 118 | switch ( $this->log_type ) { |
| 119 | case static::JSONL_LOG: |
| 120 | $result = $this->jsonl_log( $location, $message, $type ); |
| 121 | break; |
| 122 | default: |
| 123 | $result = $this->classic_log( $location, $message, $type ); |
| 124 | } |
| 125 | |
| 126 | return $result; |
| 127 | } |
| 128 | |
| 129 | private function log_info( $location, $message ) { |
| 130 | return $this->store_log( $location, $message, 'info' ); |
| 131 | } |
| 132 | |
| 133 | private function log_error( $location, $message ) { |
| 134 | return $this->store_log( $location, $message, 'error' ); |
| 135 | } |
| 136 | |
| 137 | private function log_warning( $location, $message ) { |
| 138 | return $this->store_log( $location, $message, 'warning' ); |
| 139 | } |
| 140 | |
| 141 | public function set_type( $type = FileLog::CLASSIC_LOG ) { |
| 142 | $this->log_type = $type; |
| 143 | return $this; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | public function __call( $name, $arguments ) { |
| 148 | switch ( $name ) { |
| 149 | case 'with_type': |
| 150 | return $this->set_type( ...$arguments ); |
| 151 | case 'info': |
| 152 | return $this->log_info( ...$arguments ); |
| 153 | case 'warning': |
| 154 | return $this->log_warning( ...$arguments ); |
| 155 | case 'error': |
| 156 | return $this->log_error( ...$arguments ); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | public static function __callStatic( $name, $arguments ) { |
| 161 | $instance = new static(); |
| 162 | return call_user_func_array( array( $instance, $name ), $arguments ); |
| 163 | } |
| 164 | |
| 165 | public static function get_log_files( $location ) { |
| 166 | $root = static::get_logs_root_path( $location ); |
| 167 | |
| 168 | if ( ! file_exists( $root ) ) { |
| 169 | return array(); |
| 170 | } |
| 171 | |
| 172 | $files = array_diff( scandir( $root ), array( '.', '..' ) ); |
| 173 | |
| 174 | $log_files = array(); |
| 175 | foreach ( $files as $file ) { |
| 176 | $key = filemtime( "{$root}/{$file}" ); |
| 177 | $log_files[ $key ] = "{$root}/{$file}"; |
| 178 | } |
| 179 | |
| 180 | krsort( $log_files, SORT_NUMERIC ); |
| 181 | |
| 182 | return $log_files; |
| 183 | } |
| 184 | } |
| 185 |