From 3c27d104c0ffc1a00807bad21948e3d4e4208424 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Tue, 5 Mar 2019 14:24:36 +0100 Subject: [PATCH] Add ExceptionMailerCronjob Closes #2654 --- com.woltlab.wcf/cronjob.xml | 10 ++ .../cronjob/ExceptionMailerCronjob.class.php | 96 +++++++++++++++++++ wcfsetup/install/lang/de.xml | 23 +++++ wcfsetup/install/lang/en.xml | 23 +++++ 4 files changed, 152 insertions(+) create mode 100644 wcfsetup/install/files/lib/system/cronjob/ExceptionMailerCronjob.class.php diff --git a/com.woltlab.wcf/cronjob.xml b/com.woltlab.wcf/cronjob.xml index c0dd47ff4c..affba31de8 100644 --- a/com.woltlab.wcf/cronjob.xml +++ b/com.woltlab.wcf/cronjob.xml @@ -202,5 +202,15 @@ * 0 + + wcf\system\cronjob\ExceptionMailerCronjob + Sends out notifications about new entries in the error log. + Versendet Benachrichtigungen über neue Einträge im Fehlerprotokoll. + 0 + 5 + * + * + * + diff --git a/wcfsetup/install/files/lib/system/cronjob/ExceptionMailerCronjob.class.php b/wcfsetup/install/files/lib/system/cronjob/ExceptionMailerCronjob.class.php new file mode 100644 index 0000000000..d5f502a0c8 --- /dev/null +++ b/wcfsetup/install/files/lib/system/cronjob/ExceptionMailerCronjob.class.php @@ -0,0 +1,96 @@ + + * @package WoltLabSuite\Core\System\Cronjob + * @since 5.2 + */ +class ExceptionMailerCronjob extends AbstractCronjob { + /** + * @inheritDoc + */ + public function execute(Cronjob $cronjob) { + parent::execute($cronjob); + + $timestamp = RegistryHandler::getInstance()->get('com.woltlab.wcf', 'exceptionMailerTimestamp'); + $timestamp = max($timestamp, TIME_NOW - 86400 * 3); + for ($it = $timestamp; $it < TIME_NOW; $it += 86400) { + $files[gmdate('Y-m-d', $it)] = []; + } + $files[gmdate('Y-m-d', TIME_NOW)] = []; + + $seen = []; + foreach ($files as $file => $value) { + $path = WCF_DIR.'log/'.$file.'.txt'; + if (!file_exists($path)) { + unset($files[$file]); + continue; + } + // check log size (2MiB) to prevent resource exhaustion + if (filesize($path) > 2 * (1 << 20)) { + $files[$file] = [ + 'verdict' => 'huge' + ]; + continue; + } + try { + $exceptions = ExceptionLogUtil::splitLog(file_get_contents($path)); + } + catch (\Exception $e) { + $files[$file] = [ + 'verdict' => 'corrupt' + ]; + continue; + } + + $count = 0; + $files[$file]['messages'] = []; + foreach ($exceptions as $exception) { + $exception = ExceptionLogUtil::parseException($exception); + $message = $exception['message']; + if ($exception['date'] < $timestamp) continue; + + $count++; + if (!isset($seen[$message]) && count($files[$file]['messages']) < 3) { + $files[$file]['messages'][] = StringUtil::truncate(preg_replace('/\s+/', ' ', $message), 140); + $seen[$message] = true; + } + } + if ($count == 0) { + unset($files[$file]); + continue; + } + + $files[$file]['count'] = $count; + } + + if (empty($files)) return; + + $language = LanguageFactory::getInstance()->getDefaultLanguage(); + + $email = new Email(); + $email->addRecipient(new Mailbox(MAIL_ADMIN_ADDRESS, null, $language)); + $email->setSubject($language->getDynamicVariable('wcf.acp.exceptionLog.email.subject', [ + 'date' => $timestamp + ])); + $email->setBody(new PlainTextMimePart($language->getDynamicVariable('wcf.acp.exceptionLog.email.body', [ + 'date' => $timestamp, + 'files' => $files + ]))); + $email->send(); + RegistryHandler::getInstance()->set('com.woltlab.wcf', 'exceptionMailerTimestamp', TIME_NOW); + } +} diff --git a/wcfsetup/install/lang/de.xml b/wcfsetup/install/lang/de.xml index 4d69a15693..70f1efacb4 100644 --- a/wcfsetup/install/lang/de.xml +++ b/wcfsetup/install/lang/de.xml @@ -617,6 +617,29 @@ + + diff --git a/wcfsetup/install/lang/en.xml b/wcfsetup/install/lang/en.xml index 75993346a7..e2a184b908 100644 --- a/wcfsetup/install/lang/en.xml +++ b/wcfsetup/install/lang/en.xml @@ -593,6 +593,29 @@ + + -- 2.20.1