From 152f516718425d70f287454c3968b2cdc81b591e Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Wed, 7 Apr 2021 15:57:27 -0300 Subject: [PATCH] fix: formatting bg rocesso log and error log --- .../class-tainacan-background-process.php | 31 ++++++++++--------- .../exporter/class-tainacan-exporter.php | 8 ++--- .../class-tainacan-generic-process.php | 9 ++---- .../importer/class-tainacan-importer.php | 10 ++---- 4 files changed, 25 insertions(+), 33 deletions(-) diff --git a/src/classes/class-tainacan-background-process.php b/src/classes/class-tainacan-background-process.php index abe9d75c5..2a5d1c8a9 100644 --- a/src/classes/class-tainacan-background-process.php +++ b/src/classes/class-tainacan-background-process.php @@ -285,11 +285,11 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process { $batch = $this->get_batch(); if($this->process_lock_in_time != get_site_transient( $this->identifier . '_process_lock' )) { - $this->write_log($batch->key, ['New request has ignored']); + $this->write_log($batch->key, [['datetime' => date("Y-m-d H:i:s"), 'message' => 'New request has ignored']]); wp_die(); } - - $this->write_log($batch->key, ['New Request']); + + $this->write_log($batch->key, [['datetime' => date("Y-m-d H:i:s"), 'message' => 'New Request']]); register_shutdown_function(function() use($batch) { $error = error_get_last(); @@ -309,8 +309,8 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process { $this->debug('Shutdown with Fatal error captured'); $this->debug($error_str); - $this->write_error_log($batch->key, ['Fatal Error: ' . $error_str]); - $this->write_error_log($batch->key, ['Process aborted']); + $this->write_error_log($batch->key, [['datetime' => date("Y-m-d H:i:s"), 'message' => 'Fatal Error: ' . $error_str]] ); + $this->write_error_log($batch->key, [['datetime' => date("Y-m-d H:i:s"), 'message' => 'Process aborted']]); $this->close( $batch->key, 'errored' ); $this->debug('Batch closed due to captured error'); $this->unlock_process(); @@ -324,8 +324,8 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process { $task = $this->task( $task ); } catch (\Exception $e) { // TODO: Add Stacktrace - $this->write_error_log($batch->key, ['Fatal Error: ' . $e->getMessage()]); - $this->write_error_log($batch->key, ['Process aborted']); + $this->write_error_log($batch->key, [['datetime' => date("Y-m-d H:i:s"), 'message' => 'Fatal Error: ' . $e->getMessage()]]); + $this->write_error_log($batch->key, [['datetime' => date("Y-m-d H:i:s"), 'message' => 'Process aborted']]); $task = false; $close_status = 'errored'; } @@ -353,7 +353,7 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process { } else { $this->debug('Complete'); $this->complete(); - $this->write_log($batch->key, ['Process Finished']); + $this->write_log($batch->key, [['datetime' => date("Y-m-d H:i:s"), 'message' => 'Process Finished']] ); } $this->debug('dying'); @@ -437,18 +437,21 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process { private function recursive_stingify_log_array(array $log, $break = true) { $return = ''; foreach ($log as $k => $l) { + $l_datetime = $l['datetime']; + $l_message = $l['message']; + + $return .= "[$l_datetime] "; if (!is_numeric($k)) { $return .= $k . ': '; } - if (is_array($l)) { - //$return .= $this->recursive_stingify_log_array($l, false); - $return .= print_r($l, true); - } elseif (is_string($l)) { - $return .= $l; + if (is_array($l_message)) { + //$return .= $this->recursive_stingify_log_array($l_message, false); + $return .= print_r($l_message, true); + } elseif (is_string($l_message)) { + $return .= $l_message; } $return .="\n"; //$return .= $break ? "\n" : ', '; - } return $return; diff --git a/src/classes/exporter/class-tainacan-exporter.php b/src/classes/exporter/class-tainacan-exporter.php index e1f39ead2..b1feb858e 100644 --- a/src/classes/exporter/class-tainacan-exporter.php +++ b/src/classes/exporter/class-tainacan-exporter.php @@ -355,15 +355,11 @@ abstract class Exporter { } public function add_log($message ) { - $count = count($this->log); - $date_key = sprintf("[%' 9d | %s]", $count, date("Y-m-d H:i:s")); - $this->log[$date_key] = $message; + $this->log[] = ['datetime' => date("Y-m-d H:i:s"), 'message' => $message]; } public function add_error_log($message ) { - $count = count($this->log); - $date_key = sprintf("[%' 9d | %s]", $count, date("Y-m-d H:i:s")); - $this->error_log[$date_key] = $message; + $this->error_log[] = ['datetime' => date("Y-m-d H:i:s"), 'message' => $message]; } public function is_finished() { diff --git a/src/classes/generic-background-process/class-tainacan-generic-process.php b/src/classes/generic-background-process/class-tainacan-generic-process.php index 191739898..a24c03234 100644 --- a/src/classes/generic-background-process/class-tainacan-generic-process.php +++ b/src/classes/generic-background-process/class-tainacan-generic-process.php @@ -146,14 +146,11 @@ abstract class Generic_Process { } public function add_log($message ) { - $count = count($this->log); - $date_key = sprintf("[%' 9d | %s]", $count, date("Y-m-d H:i:s")); - $this->log[$date_key] = $message; + $this->log[] = ['datetime' => date("Y-m-d H:i:s"), 'message' => $message]; } - public function add_error_log($message ){ - $date_key = '[' . date("Y-m-d H:i:s") . '] '; - $this->error_log[$date_key] = $message; + public function add_error_log($message ) { + $this->error_log[] = ['datetime' => date("Y-m-d H:i:s"), 'message' => $message]; } public function is_finished() { diff --git a/src/classes/importer/class-tainacan-importer.php b/src/classes/importer/class-tainacan-importer.php index f4d848570..02aaaf2ae 100644 --- a/src/classes/importer/class-tainacan-importer.php +++ b/src/classes/importer/class-tainacan-importer.php @@ -345,14 +345,10 @@ abstract class Importer { * @param $messagelog */ public function add_log($message ) { - $count = count($this->log); - $date_key = sprintf("[%' 9d | %s]", $count, date("Y-m-d H:i:s")); - $this->log[$date_key] = $message; + $this->log[] = ['datetime' => date("Y-m-d H:i:s"), 'message' => $message]; } - public function add_error_log($message ){ - $count = count($this->log); - $date_key = sprintf("[%' 9d | %s]", $count, date("Y-m-d H:i:s")); - $this->error_log[$date_key] = $message; + public function add_error_log($message ) { + $this->error_log[] = ['datetime' => date("Y-m-d H:i:s"), 'message' => $message]; } public function add_collection(array $collection) {