Improved email error logging

This commit is contained in:
Igor Scheller 2019-04-29 23:17:58 +02:00 committed by msquare
parent 9829e6da39
commit ca0b2d8b5f
2 changed files with 19 additions and 6 deletions

View File

@ -2,6 +2,7 @@
use Engelsystem\Mail\EngelsystemMailer; use Engelsystem\Mail\EngelsystemMailer;
use Engelsystem\Models\User\User; use Engelsystem\Models\User\User;
use Psr\Log\LogLevel;
/** /**
* @param User $recipientUser * @param User $recipientUser
@ -13,17 +14,17 @@ use Engelsystem\Models\User\User;
function engelsystem_email_to_user($recipientUser, $title, $message, $notIfItsMe = false) function engelsystem_email_to_user($recipientUser, $title, $message, $notIfItsMe = false)
{ {
if ($notIfItsMe && auth()->user()->id == $recipientUser->id) { if ($notIfItsMe && auth()->user()->id == $recipientUser->id) {
#return true; return true;
} }
/** @var \Engelsystem\Helpers\Translator $translator */ /** @var \Engelsystem\Helpers\Translator $translator */
$translator = app()->get('translator'); $translator = app()->get('translator');
$locale = $translator->getLocale(); $locale = $translator->getLocale();
try { try {
/** @var EngelsystemMailer $mailer */ /** @var EngelsystemMailer $mailer */
$mailer = app('mailer'); $mailer = app('mailer');
$translator->setLocale($recipientUser->settings->language); $translator->setLocale($recipientUser->settings->language);
$status = $mailer->sendView( $status = $mailer->sendView(
$recipientUser->contact->email ? $recipientUser->contact->email : $recipientUser->email, $recipientUser->contact->email ? $recipientUser->contact->email : $recipientUser->email,
@ -31,8 +32,15 @@ function engelsystem_email_to_user($recipientUser, $title, $message, $notIfItsMe
'emails/mail', 'emails/mail',
['username' => $recipientUser->name, 'message' => $message] ['username' => $recipientUser->name, 'message' => $message]
); );
} catch(Exception $e) { } catch (Exception $e) {
$status = 0; $status = 0;
engelsystem_log(sprintf(
'An exception occurred while sending a mail to %s in %s:%u: %s',
$recipientUser->name,
$e->getFile(),
$e->getLine(),
$e->getMessage()
), LogLevel::CRITICAL);
} }
$translator->setLocale($locale); $translator->setLocale($locale);

View File

@ -1,14 +1,19 @@
<?php <?php
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
/** /**
* Write a log entry. * Write a log entry.
* This should be used to log user's activity. * This should be used to log user's activity.
* *
* @param string $message * @param string $message
* @param string $level
*/ */
function engelsystem_log($message) function engelsystem_log($message, $level = LogLevel::INFO)
{ {
$nick = "Guest"; $nick = "Guest";
/** @var LoggerInterface $logger */
$logger = app('logger'); $logger = app('logger');
$user = auth()->user(); $user = auth()->user();
@ -16,5 +21,5 @@ function engelsystem_log($message)
$nick = User_Nick_render($user); $nick = User_Nick_render($user);
} }
$logger->info('{nick}: {message}', ['nick' => $nick, 'message' => $message]); $logger->log($level, '{nick}: {message}', ['nick' => $nick, 'message' => $message]);
} }