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.
This commit is contained in:
Paul B 2019-04-15 15:56:24 +02:00
parent 5f7388cddb
commit e8620026cf
No known key found for this signature in database
GPG key ID: DE331B23748D3A27
2 changed files with 39 additions and 6 deletions

View file

@ -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);

View file

@ -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);
}
}