From e8620026cf4e3f97363740d5015b5739b1b2ed2b Mon Sep 17 00:00:00 2001 From: Paul B Date: Mon, 15 Apr 2019 15:56:24 +0200 Subject: [PATCH] refacto(logService): add a new logEntries method to the log service As a followup to #362 this is a small addition to the log service when we want to log a list of entries into a single message. The log service can itself make sure to escape commas instead of expecting callers to do so. --- .../Framadate/Services/AdminPollService.php | 29 +++++++++++++++---- app/classes/Framadate/Services/LogService.php | 16 +++++++++- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/app/classes/Framadate/Services/AdminPollService.php b/app/classes/Framadate/Services/AdminPollService.php index 62912c8..e86bbb8 100644 --- a/app/classes/Framadate/Services/AdminPollService.php +++ b/app/classes/Framadate/Services/AdminPollService.php @@ -91,7 +91,15 @@ class AdminPollService { */ function deleteEntirePoll($poll_id) { $poll = $this->pollRepository->findById($poll_id); - $this->logService->log('DELETE_POLL', "id:$poll->id, format:$poll->format, admin:$poll->admin_name, mail:$poll->admin_mail"); + $this->logService->logEntries( + 'DELETE_POLL', + [ + "id:$poll->id", + "format:$poll->format", + "admin:$poll->admin_name", + "mail:$poll->admin_mail" + ] + ); // Delete the entire poll $this->voteRepository->deleteByPollId($poll_id); @@ -110,7 +118,10 @@ class AdminPollService { * @return bool true if action succeeded */ public function deleteDateSlot($poll, $slot) { - $this->logService->log('DELETE_SLOT', 'id:' . $poll->id . ', slot:' . json_encode($slot)); + $this->logService->logEntries( + 'DELETE_SLOT', + ["id:$poll->id", 'slot:' . json_encode($slot)] + ); $datetime = $slot->title; $moment = $slot->moment; @@ -158,7 +169,9 @@ class AdminPollService { } public function deleteClassicSlot($poll, $slot_title) { - $this->logService->log('DELETE_SLOT', 'id:' . $poll->id . ', slot:' . $slot_title); + $this->logService->logEntries( + ['DELETE_SLOT', "id:$poll->id", "slot:$slot_title"] + ); $slots = $this->pollService->allSlotsByPoll($poll); @@ -200,7 +213,10 @@ class AdminPollService { * @throws \Doctrine\DBAL\ConnectionException */ public function addDateSlot($poll_id, $datetime, $new_moment) { - $this->logService->log('ADD_COLUMN', 'id:' . $poll_id . ', datetime:' . $datetime . ', moment:' . str_replace(',', '-', $new_moment)); + $this->logService->logEntries( + 'ADD_COLUMN', + ["id:$poll_id", "datetime:$datetime", "moment:$new_moment"] + ); try { $slots = $this->slotRepository->listByPollId($poll_id); @@ -252,7 +268,10 @@ class AdminPollService { * @throws \Doctrine\DBAL\DBALException */ public function addClassicSlot($poll_id, $title) { - $this->logService->log('ADD_COLUMN', 'id:' . $poll_id . ', title:' . str_replace(',', '-', $title)); + $this->logService->logEntries( + 'ADD_COLUMN', + ["id:$poll_id", "title:$title"] + ); $slots = $this->slotRepository->listByPollId($poll_id); diff --git a/app/classes/Framadate/Services/LogService.php b/app/classes/Framadate/Services/LogService.php index 9242df1..1f703d7 100644 --- a/app/classes/Framadate/Services/LogService.php +++ b/app/classes/Framadate/Services/LogService.php @@ -19,5 +19,19 @@ class LogService { function log($tag, $message) { error_log(date('Ymd His') . ' [' . $tag . '] ' . $message . "\n", 3, ROOT_DIR . LOG_FILE); } + + /** + * Log a list of entries as a single message to the log file. + * + * @param $tag string A tag is used to quickly found a message when reading log file + * @param $entries array some entries to join with comma into a single message + */ + function logEntries($tag, $entries) { + $escapeCommas = function($value) { + return str_replace(',', '-', $value); + }; + $message = join(', ', array_map($escapeCommas, $entries)); + + error_log(date('Ymd His') . ' [' . $tag . '] ' . $message . "\n", 3, ROOT_DIR . LOG_FILE); + } } - \ No newline at end of file