58 public static function init()
60 if (!self::$shutdownRegistered) {
61 register_shutdown_function([__CLASS__,
'flush']);
62 self::$shutdownRegistered =
true;
78 private static function write($data, $type, $file =
null)
84 error_log(
'[' . $type .
'] ' . $data);
93 $file = $type === self::ERROR
120 self::$logBuffer[] = [
126 self::$logStats[
'buffered']++;
129 if ($type === self::ERROR || count(self::$logBuffer) >= self::$logBufferSize) {
144 if (empty(self::$logBuffer)) {
152 foreach (self::$logBuffer as $log) {
153 error_log(
'[' . date(
'Y-m-d H:i:s', $log[
'time']) .
'] [' . $log[
'type'] .
'] ' . $log[
'data']);
155 self::$logStats[
'flushed'] += count(self::$logBuffer);
156 self::$logBuffer = [];
162 foreach (self::$logBuffer as $log) {
163 $logsByFile[$log[
'file']][] = $log;
166 foreach ($logsByFile as $file => $logs) {
168 foreach ($logs as $log) {
169 $content .=
'[' . date(
'Y-m-d H:i:s', $log[
'time']) .
'] # ' .
171 $log[
'type'] .
': ' . $log[
'data'] . PHP_EOL;
174 $written = @file_put_contents($file, $content, FILE_APPEND | LOCK_EX);
175 if ($written ===
false) {
177 foreach ($logs as $log) {
178 error_log(
'[' . $log[
'type'] .
'] ' . $log[
'data'] .
' (target: ' . $file .
')');
182 self::$logStats[
'writes']++;
185 self::$logStats[
'flushed'] += count(self::$logBuffer);
186 self::$logBuffer = [];
197 self::$logBuffer = [];
198 self::$shutdownRegistered =
false;
213 return self::$logStats;
224 if ($size > 0 && $size <= 1000) {
225 self::$logBufferSize = $size;
236 return self::$logBufferSize;
258 $separator =
'========================================================================================' . PHP_EOL;
259 foreach ($logs as $log) {
260 if (!file_exists($log)) {
263 $logContent = @file_get_contents($log);
264 if ($logContent !==
false && !str_ends_with($logContent, $separator)) {
265 file_put_contents($log, $separator, FILE_APPEND);
276 public static function trace($data, $file =
null)
287 public static function debug($data, $file =
null)
298 public static function info($data, $file =
null)
309 public static function warning($data, $file =
null)
321 public static function error($data, $file =
null)
343 self::trace(
'Reload ' . get_class($classInstance));
static setBufferSize($size)
static info($data, $file=null)
static debug($data, $file=null)
static warning($data, $file=null)
static reloadClass($classInstance)
static write($data, $type, $file=null)
static $shutdownRegistered
static trace($data, $file=null)
static error($data, $file=null)
static initClass($classInstance)