Просмотр исходного кода

Add logging of document emails sent

Alan Lam 3 лет назад
Родитель
Сommit
403107dfa6
5 измененных файлов с 144 добавлено и 2 удалено
  1. 2 1
      composer.json
  2. 103 1
      composer.lock
  3. 2 0
      controllers/document.php
  4. 20 0
      engine/Core.php
  5. 17 0
      router.php

+ 2 - 1
composer.json

@@ -29,7 +29,8 @@
         "robmorgan/phinx": "^0.12.10",
         "vlucas/phpdotenv": "^5.4",
 		"ext-bcmath": "*",
-        "phpmailer/phpmailer": "^6.7"
+        "phpmailer/phpmailer": "^6.7",
+        "monolog/monolog": "^2.9"
 	},
 	"require-dev": {
 		"roave/security-advisories": "dev-latest"

+ 103 - 1
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "6ca30ffdfaa2756167d8b62fc9810f84",
+    "content-hash": "44e001303210e6e1fbbfdb292a79481c",
     "packages": [
         {
             "name": "cakephp/core",
@@ -519,6 +519,108 @@
             ],
             "time": "2021-11-21T21:41:47+00:00"
         },
+        {
+            "name": "monolog/monolog",
+            "version": "2.9.1",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/Seldaek/monolog.git",
+                "reference": "f259e2b15fb95494c83f52d3caad003bbf5ffaa1"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/Seldaek/monolog/zipball/f259e2b15fb95494c83f52d3caad003bbf5ffaa1",
+                "reference": "f259e2b15fb95494c83f52d3caad003bbf5ffaa1",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=7.2",
+                "psr/log": "^1.0.1 || ^2.0 || ^3.0"
+            },
+            "provide": {
+                "psr/log-implementation": "1.0.0 || 2.0.0 || 3.0.0"
+            },
+            "require-dev": {
+                "aws/aws-sdk-php": "^2.4.9 || ^3.0",
+                "doctrine/couchdb": "~1.0@dev",
+                "elasticsearch/elasticsearch": "^7 || ^8",
+                "ext-json": "*",
+                "graylog2/gelf-php": "^1.4.2 || ^2@dev",
+                "guzzlehttp/guzzle": "^7.4",
+                "guzzlehttp/psr7": "^2.2",
+                "mongodb/mongodb": "^1.8",
+                "php-amqplib/php-amqplib": "~2.4 || ^3",
+                "phpspec/prophecy": "^1.15",
+                "phpstan/phpstan": "^0.12.91",
+                "phpunit/phpunit": "^8.5.14",
+                "predis/predis": "^1.1 || ^2.0",
+                "rollbar/rollbar": "^1.3 || ^2 || ^3",
+                "ruflin/elastica": "^7",
+                "swiftmailer/swiftmailer": "^5.3|^6.0",
+                "symfony/mailer": "^5.4 || ^6",
+                "symfony/mime": "^5.4 || ^6"
+            },
+            "suggest": {
+                "aws/aws-sdk-php": "Allow sending log messages to AWS services like DynamoDB",
+                "doctrine/couchdb": "Allow sending log messages to a CouchDB server",
+                "elasticsearch/elasticsearch": "Allow sending log messages to an Elasticsearch server via official client",
+                "ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)",
+                "ext-curl": "Required to send log messages using the IFTTTHandler, the LogglyHandler, the SendGridHandler, the SlackWebhookHandler or the TelegramBotHandler",
+                "ext-mbstring": "Allow to work properly with unicode symbols",
+                "ext-mongodb": "Allow sending log messages to a MongoDB server (via driver)",
+                "ext-openssl": "Required to send log messages using SSL",
+                "ext-sockets": "Allow sending log messages to a Syslog server (via UDP driver)",
+                "graylog2/gelf-php": "Allow sending log messages to a GrayLog2 server",
+                "mongodb/mongodb": "Allow sending log messages to a MongoDB server (via library)",
+                "php-amqplib/php-amqplib": "Allow sending log messages to an AMQP server using php-amqplib",
+                "rollbar/rollbar": "Allow sending log messages to Rollbar",
+                "ruflin/elastica": "Allow sending log messages to an Elastic Search server"
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-main": "2.x-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Monolog\\": "src/Monolog"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "Jordi Boggiano",
+                    "email": "j.boggiano@seld.be",
+                    "homepage": "https://seld.be"
+                }
+            ],
+            "description": "Sends your logs to files, sockets, inboxes, databases and various web services",
+            "homepage": "https://github.com/Seldaek/monolog",
+            "keywords": [
+                "log",
+                "logging",
+                "psr-3"
+            ],
+            "support": {
+                "issues": "https://github.com/Seldaek/monolog/issues",
+                "source": "https://github.com/Seldaek/monolog/tree/2.9.1"
+            },
+            "funding": [
+                {
+                    "url": "https://github.com/Seldaek",
+                    "type": "github"
+                },
+                {
+                    "url": "https://tidelift.com/funding/github/packagist/monolog/monolog",
+                    "type": "tidelift"
+                }
+            ],
+            "time": "2023-02-06T13:44:46+00:00"
+        },
         {
             "name": "nesbot/carbon",
             "version": "2.58.0",

+ 2 - 0
controllers/document.php

@@ -494,6 +494,8 @@ $controller = new class($document_type, $document_number) {
 
 			$mailer->send();
 
+			$core->getLogger()->info(sprintf("%s emailed to %s by %s <%s>", $document->getTitle(), $document->contact_email, $user->getDisplayName(), $user->email));
+
 			Json::success();
 		}
 		catch(Exception $e) {

+ 20 - 0
engine/Core.php

@@ -6,6 +6,7 @@ use Exception;
 use Gettext\Loader\MoLoader;
 use Gettext\Translator;
 use Gettext\TranslatorFunctions;
+use Monolog\Logger;
 use RuntimeException;
 
 class Core {
@@ -39,6 +40,9 @@ class Core {
 	/** @var string[] CSS stylesheets */
 	protected $stylesheets = [];
 
+	/** @var Logger */
+	protected $logger;
+
 	// Static methods
 	public static function getInstance(): Core {
 		if(self::$instance === null) {
@@ -271,4 +275,20 @@ class Core {
 		return file_put_contents("settings.json", json_encode($settings));
 	}
 
+	/**
+	 * @param Logger $logger
+	 */
+	public function setLogger(Logger $logger): void {
+		$this->logger = $logger;
+	}
+
+	/**
+	 * @return Logger
+	 */
+	public function getLogger(): Logger {
+		return $this->logger;
+	}
+
+
+
 }

+ 17 - 0
router.php

@@ -3,6 +3,9 @@
 namespace App;
 
 use Exception;
+use Monolog\Formatter\LineFormatter;
+use Monolog\Handler\RotatingFileHandler;
+use Monolog\Logger;
 use RuntimeException;
 
 $router = new Router();
@@ -13,6 +16,20 @@ $router->loadRoutes();
  */
 
 $core = Core::getInstance();
+
+// Initialise logger
+if(!@mkdir("./logs") && !is_dir("./logs")) {
+	throw new RuntimeException("Failed to create logs directory.");
+}
+
+$formatter = new LineFormatter();
+$formatter->ignoreEmptyContextAndExtra();
+
+$handler = new RotatingFileHandler("./logs/site.log");
+$handler->setFormatter($formatter);
+
+$core->setLogger(new Logger("Site", [$handler]));
+
 $user = $core->getUser();
 
 $path = $_REQUEST["path"] ?? "";