PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.0.0
Kubio AI Page Builder v2.0.0
2.8.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 2.0.0 2.1.1 2.1.2 2.1.3 2.2.0 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.5 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 2.6.5 2.6.6 2.6.7 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0
kubio / lib / src / FileLog.php
kubio / lib / src Last commit date
CLI 2 years ago Core 2 years ago DemoSites 2 years ago AssetsDependencyInjector.php 3 years ago Config.php 3 years ago FileLog.php 2 years ago Flags.php 2 years ago GoogleFontsLocalLoader.php 2 years ago Migrations.php 4 years ago NotificationsManager.php 3 years ago PluginsManager.php 2 years ago
FileLog.php
184 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 if ( file_exists( $file_path ) && ! is_writable( $file_path ) ) {
56 return false;
57 }
58
59 return $file_path;
60 }
61
62 private function classic_log( $location, $message, $type = 'info' ) {
63
64 $file_path = $this->prepare_file( $location, $type );
65
66 if ( ! $file_path ) {
67 return;
68 }
69
70 $response = file_put_contents(
71 $file_path,
72 sprintf(
73 "LOG: %s ---------- %s ----------\n\n\n%s\n\n",
74 str_pad( $type, 6 ),
75 gmdate( 'Y-m-d H:i:s' ),
76 is_string( $message ) ? trim( $message ) : var_export( $message, true )
77 ),
78 FILE_APPEND
79 );
80
81 if ( $response === false ) {
82 return false;
83 }
84
85 return true;
86 }
87
88 private function jsonl_log( $location, $message, $type = 'info' ) {
89 $file_path = $this->prepare_file( $location, $type );
90
91 if ( ! $file_path ) {
92 return;
93 }
94
95 $content = array(
96 'type' => $type,
97 'date' => time(),
98 'data' => $message,
99 );
100
101 $response = file_put_contents(
102 $file_path,
103 json_encode( $content ) . "\n",
104 FILE_APPEND
105 );
106
107 if ( $response === false ) {
108 return false;
109 }
110
111 return true;
112
113 }
114
115 private function store_log( $location, $message, $type = 'info' ) {
116
117 switch ( $this->log_type ) {
118 case static::JSONL_LOG:
119 $result = $this->jsonl_log( $location, $message, $type );
120 break;
121 default:
122 $result = $this->classic_log( $location, $message, $type );
123 }
124
125 return $result;
126 }
127
128 private function log_info( $location, $message ) {
129 return $this->store_log( $location, $message, 'info' );
130 }
131
132 private function log_error( $location, $message ) {
133 return $this->store_log( $location, $message, 'error' );
134 }
135
136 private function log_warning( $location, $message ) {
137 return $this->store_log( $location, $message, 'warning' );
138 }
139
140 public function set_type( $type = FileLog::CLASSIC_LOG ) {
141 $this->log_type = $type;
142 return $this;
143 }
144
145
146 public function __call( $name, $arguments ) {
147 switch ( $name ) {
148 case 'with_type':
149 return $this->set_type( ...$arguments );
150 case 'info':
151 return $this->log_info( ...$arguments );
152 case 'warning':
153 return $this->log_warning( ...$arguments );
154 case 'error':
155 return $this->log_error( ...$arguments );
156 }
157 }
158
159 public static function __callStatic( $name, $arguments ) {
160 $instance = new static();
161 return call_user_func_array( array( $instance, $name ), $arguments );
162 }
163
164 public static function get_log_files( $location ) {
165 $root = static::get_logs_root_path( $location );
166
167 if ( ! file_exists( $root ) ) {
168 return array();
169 }
170
171 $files = array_diff( scandir( $root ), array( '.', '..' ) );
172
173 $log_files = array();
174 foreach ( $files as $file ) {
175 $key = filemtime( "{$root}/{$file}" );
176 $log_files[ $key ] = "{$root}/{$file}";
177 }
178
179 krsort( $log_files, SORT_NUMERIC );
180
181 return $log_files;
182 }
183 }
184