From 33eb4021fa9e0c108dc3f7d1b1e4c266d623f526 Mon Sep 17 00:00:00 2001 From: Miguel N Date: Sun, 27 Dec 2020 19:26:38 +0000 Subject: [PATCH] Added proper logging and error handling --- .gitignore | 3 +- bot.php | 5 +-- composer.json | 3 +- composer.lock | 73 ++++++++++++++++++++++++++++++- src/Bot/Actions/MessageLogger.php | 9 +++- src/Bot/Commands/Gimme.php | 10 +++++ src/Bot/Commands/Help.php | 5 +++ src/Bot/Commands/NumberFact.php | 6 +++ src/Bot/Helpers/CatAPI.php | 60 +++++++++++++++++++------ src/Bot/init.php | 7 +++ 10 files changed, 160 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index b44d4eb..34a1ee9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ vendor/ -.env \ No newline at end of file +.env +Storage/Logs/*.log \ No newline at end of file diff --git a/bot.php b/bot.php index bf0f681..c2bbfa9 100755 --- a/bot.php +++ b/bot.php @@ -1,6 +1,7 @@ #!/usr/bin/env php initialize() ->addCommand([ diff --git a/composer.json b/composer.json index 71a47a2..d2f2f98 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,8 @@ "team-reflex/discord-php": "^5.0", "vlucas/phpdotenv": "^5.2", "kitetail/zttp": "^0.6.0", - "ely/mojang-api": "^0.2.1" + "ely/mojang-api": "^0.2.1", + "analog/analog": "^1.0" }, "require-dev": { "monolog/monolog": "^2.1" diff --git a/composer.lock b/composer.lock index 602ab2a..6a18435 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,77 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e9cc6619e8c1bb4e6546fcb9e369bcd4", + "content-hash": "e92633d7a29521137b4726e3060ecc6d", "packages": [ + { + "name": "analog/analog", + "version": "1.0.17-stable", + "source": { + "type": "git", + "url": "https://github.com/jbroadway/analog.git", + "reference": "f9541cdf30dfb391a8be1befb05a3344099e6ea9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/jbroadway/analog/zipball/f9541cdf30dfb391a8be1befb05a3344099e6ea9", + "reference": "f9541cdf30dfb391a8be1befb05a3344099e6ea9", + "shasum": "" + }, + "require": { + "php": ">=7.2", + "psr/log": "^1.0.1" + }, + "require-dev": { + "phpunit/phpunit": "^8.5" + }, + "type": "library", + "autoload": { + "psr-0": { + "Analog": "lib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Johnny Broadway", + "email": "johnny@johnnybroadway.com", + "homepage": "http://www.johnnybroadway.com/" + } + ], + "description": "Fast, flexible, easy PSR-3-compatible PHP logging package with dozens of handlers.", + "homepage": "https://github.com/jbroadway/analog", + "keywords": [ + "PSR3", + "alerts", + "amon", + "apprise", + "chrome-logger", + "console", + "debug", + "debugging", + "error", + "errors", + "firephp", + "gelf", + "log", + "logger", + "logging", + "logs", + "monitor", + "monitoring", + "psr-3", + "stderr", + "syslog" + ], + "support": { + "issues": "https://github.com/jbroadway/analog/issues", + "source": "https://github.com/jbroadway/analog/tree/1.0.17-stable" + }, + "time": "2020-12-16T17:57:17+00:00" + }, { "name": "brick/math", "version": "0.9.1", @@ -3432,5 +3501,5 @@ "prefer-lowest": false, "platform": [], "platform-dev": [], - "plugin-api-version": "1.1.0" + "plugin-api-version": "2.0.0" } diff --git a/src/Bot/Actions/MessageLogger.php b/src/Bot/Actions/MessageLogger.php index b736570..76db625 100644 --- a/src/Bot/Actions/MessageLogger.php +++ b/src/Bot/Actions/MessageLogger.php @@ -2,14 +2,21 @@ namespace nogueiracodes\RaspberryBot\Bot\Actions; +use Analog\Analog; use nogueiracodes\RaspberryBot\Core\Interfaces\Action; class MessageLogger implements Action { + + public function __construct() + { + Analog::info('Actions: Loaded MessageLogger!'); + } + public function processAction($message) { - echo "Sniffed message from " . $message->author->username . ": " . $message->content . PHP_EOL; + Analog::info("Siffer: Sniffed message from " . $message->author->username . ": " . $message->content) } diff --git a/src/Bot/Commands/Gimme.php b/src/Bot/Commands/Gimme.php index 7e466c9..bffd73e 100644 --- a/src/Bot/Commands/Gimme.php +++ b/src/Bot/Commands/Gimme.php @@ -2,6 +2,7 @@ namespace nogueiracodes\RaspberryBot\Bot\Commands; +use Analog\Analog; use Exception; use nogueiracodes\RaspberryBot\Bot\Helpers\CatAPI; use nogueiracodes\RaspberryBot\Core\Interfaces\Command; @@ -20,10 +21,19 @@ class Gimme implements Command public function __construct() { $this->cats = new CatAPI(); + Analog::info('Commands: Loaded Gimme!'); } public function run(array $parameters) { + Analog::debug('Gimme: Nr of args: ' . count($parameters)); + + + + Analog::debug('Gimme: Thing: ' . $parameters['thing'] ?? 'unset'); + Analog::debug('Gimme: Count: ' . $parameters['quantity'] ?? 'unset'); + Analog::debug('Gimme: Raw parameter contents: ', json_encode($parameters)); + if ($parameters['quantity'] > 25) { throw new Exception('Sorry, but I can\'t send you more than 25 cat pictures at a time. What a cat lover!'); diff --git a/src/Bot/Commands/Help.php b/src/Bot/Commands/Help.php index f83d152..19f6571 100644 --- a/src/Bot/Commands/Help.php +++ b/src/Bot/Commands/Help.php @@ -2,6 +2,7 @@ namespace nogueiracodes\RaspberryBot\Bot\Commands; +use Analog\Analog; use Exception; use nogueiracodes\RaspberryBot\Core\Interfaces\Command; use nogueiracodes\RaspberryBot\Traits\HasSignature; @@ -14,6 +15,10 @@ class Help implements Command public $signature = "help {section: The section to retrieve help for}"; + public function __construct() + { + Analog::info('Commands: Loaded Help!'); + } public function run(array $parameters) { diff --git a/src/Bot/Commands/NumberFact.php b/src/Bot/Commands/NumberFact.php index be79a78..b31806b 100644 --- a/src/Bot/Commands/NumberFact.php +++ b/src/Bot/Commands/NumberFact.php @@ -2,6 +2,7 @@ namespace nogueiracodes\RaspberryBot\Bot\Commands; +use Analog\Analog; use Exception; use nogueiracodes\RaspberryBot\Core\Interfaces\Command; use nogueiracodes\RaspberryBot\Traits\HasSignature; @@ -15,6 +16,11 @@ class NumberFact implements Command public $signature = "numberfact {number: The number for which you want facts for.}"; + public function __construct() + { + Analog::log('Commands: Loaded NumberFact!'); + } + public function run(array $parameters) { diff --git a/src/Bot/Helpers/CatAPI.php b/src/Bot/Helpers/CatAPI.php index e2e87c4..292763a 100644 --- a/src/Bot/Helpers/CatAPI.php +++ b/src/Bot/Helpers/CatAPI.php @@ -7,6 +7,7 @@ use GuzzleHttp\Exception\ClientException; namespace nogueiracodes\RaspberryBot\Bot\Helpers; +use Analog\Analog; use Exception; use InvalidArgumentException; use Zttp\Zttp; @@ -18,37 +19,69 @@ class CatAPI { private $baseURL = "https://api.thecatapi.com/v1/"; + + private $hardLimit = 25; + public function __construct() { - $this->headers = [ - 'x-api-key' => $_ENV['CAT_API_KEY'] - ]; + Analog::info('Helpers: Loaded new instance of CatAPI!'); + + if (isset($_ENV['CAT_API_KEY'])) + { + $this->headers = [ + 'x-api-key' => $_ENV['CAT_API_KEY'] + ]; + } + else + { + $this->hardLimit = 1; + Analog::urgent('CatAPI ERROR: Failed to load Cat API key. Results limited to 1 picture. It is recommended set an API key.'); + } } public function getCatPicture($count = 1, $size = "small"): string { + Analog::debug('CatAPI: Fetching cat picture with count ' . $count . ' and size ' . $size); + // anti smart-ass device - if ($count = 0) - $count = 1; - - $catPic = Zttp::withHeaders($this->headers)->get($this->baseURL . 'images/search' . http_build_query([ - 'limit' => $count, - 'size' => $size, - 'has_breeds' => false - ])); - - if ($catPic->isOk()) + if ($count > 0 && $count <= $this->hardLimit) { + Analog::error('CatAPI: Invalid arguments: Count of ' . $count . ' is over the hard limit of ' . $this->hardLimit); + throw new Exception("Unacceptable quantity: " . $count . ". Please try again with no more than " . $this->hardLimit . " picture(s)."); + } + + if (!empty($this->headers)) + { + $catPic = Zttp::withHeaders($this->headers)->get($this->baseURL . 'images/search', [ + 'limit' => $count, + 'size' => $size, + 'has_breeds' => false + ]); + } + else + { + $catPic = Zttp::get($this->baseURL . 'images/search', [ + 'limit' => $count, + 'size' => $size, + 'has_breeds' => false + ]); + } + + if ($catPic->isSuccess()) + { + $results = $catPic->json(); $pics = []; if (count($results) == 1) { + Analog::debug('CatAPI: Fetched single cat picture.'); return $results[0]['url']; } foreach($results as $result) { + Analog::debug('CatAPI: Fetched multiple cat pictures; Splitting into EOL divided string.'); array_push($pics, $result['url']); } @@ -57,6 +90,7 @@ class CatAPI { } else { + Analog::error('CatAPI: Network error. Failed to fetch cat picture. Request status code: ' . $catPic->status()); throw new Exception('Sorry, but I couldn\'t get you a good cat picture this time. Try again later!'); } diff --git a/src/Bot/init.php b/src/Bot/init.php index 640cb38..464c926 100644 --- a/src/Bot/init.php +++ b/src/Bot/init.php @@ -1,5 +1,8 @@ required('COMMAND_PREFIX')->notEmpty(); $dotenv->required('CAT_API_KEY')->notEmpty(); define('PROJECT_ROOT', realpath(__DIR__ . '/../../')); + +Analog::handler(File::init(PROJECT_ROOT . 'Storage/Logs/bot.log')); + +Analog::info("Finished intializing configuration and dependencies.");