From ac56c13765fb15a1edbb2a2953f2a4b4a257ce50 Mon Sep 17 00:00:00 2001 From: Rico van Zelst Date: Mon, 3 Jun 2024 17:07:05 +0200 Subject: [PATCH] feat: Add Discord logging support - Added Discord webhook URL to .env.example - Included 'marvinlabs/laravel-discord-logger' in composer.json - Created config file for Discord logger settings --- .env.example | 4 +- composer.json | 1 + composer.lock | 72 +++++++++++++++++++- config/discord-logger.php | 61 +++++++++++++++++ config/logging.php | 140 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 276 insertions(+), 2 deletions(-) create mode 100644 config/discord-logger.php create mode 100644 config/logging.php diff --git a/.env.example b/.env.example index 831bd45..a5c9318 100644 --- a/.env.example +++ b/.env.example @@ -5,6 +5,8 @@ APP_DEBUG=true APP_TIMEZONE=Europe/Amsterdam APP_URL=http://127.0.0.1:8000 +LOG_DISCORD_WEBHOOK_URL=https://discord.com/api/webhooks/ + APP_LOCALE=en APP_FALLBACK_LOCALE=en APP_FAKER_LOCALE=en_US @@ -36,7 +38,7 @@ HCAPTCHA_SECRET=secret-key HCAPTCHA_SITEKEY=site-key LOG_CHANNEL=stack -LOG_STACK=single +LOG_STACK=single,discord LOG_DEPRECATIONS_CHANNEL=null LOG_LEVEL=debug diff --git a/composer.json b/composer.json index f3cac14..2cee0e6 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,7 @@ "laravel/sanctum": "^4.0", "laravel/tinker": "^2.9", "league/flysystem-aws-s3-v3": "^3.0", + "marvinlabs/laravel-discord-logger": "^1.4", "saade/blade-iconsax": "^1.2", "scyllaly/hcaptcha": "dev-l11-compatibility", "spatie/laravel-backup": "^8.6", diff --git a/composer.lock b/composer.lock index fbc89ff..36eff48 100644 --- a/composer.lock +++ b/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": "300044012c721a5f7c149e7c0f8c3aa0", + "content-hash": "01991fbc1fe35d4ff8b7fec2328024e4", "packages": [ { "name": "andcarpi/laravel-popper", @@ -2962,6 +2962,76 @@ ], "time": "2024-03-14T14:03:32+00:00" }, + { + "name": "marvinlabs/laravel-discord-logger", + "version": "v1.4.2", + "source": { + "type": "git", + "url": "https://github.com/marvinlabs/laravel-discord-logger.git", + "reference": "e8a53579dcb4650cf1c362829f90cb9580249209" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/marvinlabs/laravel-discord-logger/zipball/e8a53579dcb4650cf1c362829f90cb9580249209", + "reference": "e8a53579dcb4650cf1c362829f90cb9580249209", + "shasum": "" + }, + "require": { + "ext-json": "*", + "guzzlehttp/guzzle": "^6.3|^7.0", + "illuminate/support": "^7.0|^8.0|^9.0|^10.0 | ^11.0", + "php": ">=7.3 | ^8.0" + }, + "require-dev": { + "mockery/mockery": "^1.0", + "orchestra/testbench": "^5.0|^6.0|^8.0 | ^9.0", + "phpunit/phpunit": "^8.0|^9.0 | ^10.5", + "roave/security-advisories": "dev-master" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "MarvinLabs\\DiscordLogger\\ServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "MarvinLabs\\DiscordLogger\\": "src/DiscordLogger" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Vincent Mimoun-Prat", + "email": "contact@vincentprat.info", + "homepage": "https://vincentprat.info", + "role": "Developer" + } + ], + "description": "Logging to a discord channel in Laravel", + "keywords": [ + "discord", + "laravel", + "logger", + "logging" + ], + "support": { + "issues": "https://github.com/marvinlabs/laravel-discord-logger/issues", + "source": "https://github.com/marvinlabs/laravel-discord-logger/tree/v1.4.2" + }, + "funding": [ + { + "url": "https://github.com/vpratfr", + "type": "github" + } + ], + "time": "2024-03-25T08:51:09+00:00" + }, { "name": "masterminds/html5", "version": "2.8.1", diff --git a/config/discord-logger.php b/config/discord-logger.php new file mode 100644 index 0000000..c3d155b --- /dev/null +++ b/config/discord-logger.php @@ -0,0 +1,61 @@ + [ + 'name' => env('APP_NAME', 'Heimerdinger Logger'), + 'avatar_url' => null, + ], + + /** + * The converter to use to turn a log record into a discord message + * + * Bundled converters: + * - \MarvinLabs\DiscordLogger\Converters\SimpleRecordConverter::class + * - \MarvinLabs\DiscordLogger\Converters\RichRecordConverter::class + */ + 'converter' => \MarvinLabs\DiscordLogger\Converters\RichRecordConverter::class, + + /** + * If enabled, stacktraces will be attached as files. If not, stacktraces will be directly printed out in the + * message. + * + * Valid values are: + * + * - 'smart': when stacktrace is less than 2000 characters, it is inlined with the message, else attached as file + * - 'file': stacktrace is always attached as file + * - 'inline': stacktrace is always inlined with the message, truncated if necessary + */ + 'stacktrace' => 'smart', + + /* + * A set of colors to associate to the different log levels when using the `RichRecordConverter` + */ + 'colors' => [ + 'DEBUG' => 0x607d8b, + 'INFO' => 0x4caf50, + 'NOTICE' => 0x2196f3, + 'WARNING' => 0xff9800, + 'ERROR' => 0xf44336, + 'CRITICAL' => 0xe91e63, + 'ALERT' => 0x673ab7, + 'EMERGENCY' => 0x9c27b0, + ], + + /* + * A set of emojis to associate to the different log levels. Set to null to disable an emoji for a given level + */ + 'emojis' => [ + 'DEBUG' => ':beetle:', + 'INFO' => ':bulb:', + 'NOTICE' => ':wink:', + 'WARNING' => ':flushed:', + 'ERROR' => ':poop:', + 'CRITICAL' => ':imp:', + 'ALERT' => ':japanese_ogre:', + 'EMERGENCY' => ':skull:', + ], +]; diff --git a/config/logging.php b/config/logging.php new file mode 100644 index 0000000..076cffc --- /dev/null +++ b/config/logging.php @@ -0,0 +1,140 @@ + env('LOG_CHANNEL', 'stack'), + + /* + |-------------------------------------------------------------------------- + | Deprecations Log Channel + |-------------------------------------------------------------------------- + | + | This option controls the log channel that should be used to log warnings + | regarding deprecated PHP and library features. This allows you to get + | your application ready for upcoming major versions of dependencies. + | + */ + + 'deprecations' => [ + 'channel' => env('LOG_DEPRECATIONS_CHANNEL', 'null'), + 'trace' => env('LOG_DEPRECATIONS_TRACE', false), + ], + + /* + |-------------------------------------------------------------------------- + | Log Channels + |-------------------------------------------------------------------------- + | + | Here you may configure the log channels for your application. Laravel + | utilizes the Monolog PHP logging library, which includes a variety + | of powerful log handlers and formatters that you're free to use. + | + | Available Drivers: "single", "daily", "slack", "syslog", + | "errorlog", "monolog", "custom", "stack" + | + */ + + 'channels' => [ + + 'stack' => [ + 'driver' => 'stack', + 'channels' => explode(',', env('LOG_STACK', 'single')), + 'ignore_exceptions' => false, + ], + + 'single' => [ + 'driver' => 'single', + 'path' => storage_path('logs/laravel.log'), + 'level' => env('LOG_LEVEL', 'debug'), + 'replace_placeholders' => true, + ], + + 'daily' => [ + 'driver' => 'daily', + 'path' => storage_path('logs/laravel.log'), + 'level' => env('LOG_LEVEL', 'debug'), + 'days' => env('LOG_DAILY_DAYS', 14), + 'replace_placeholders' => true, + ], + + 'slack' => [ + 'driver' => 'slack', + 'url' => env('LOG_SLACK_WEBHOOK_URL'), + 'username' => env('LOG_SLACK_USERNAME', 'Laravel Log'), + 'emoji' => env('LOG_SLACK_EMOJI', ':boom:'), + 'level' => env('LOG_LEVEL', 'critical'), + 'replace_placeholders' => true, + ], + + 'discord' => [ + 'driver' => 'custom', + 'via' => \MarvinLabs\DiscordLogger\Logger::class, + 'level' => 'debug', + 'url' => env('LOG_DISCORD_WEBHOOK_URL'), + 'ignore_exceptions' => env('LOG_DISCORD_IGNORE_EXCEPTIONS', false), + ], + + 'papertrail' => [ + 'driver' => 'monolog', + 'level' => env('LOG_LEVEL', 'debug'), + 'handler' => env('LOG_PAPERTRAIL_HANDLER', SyslogUdpHandler::class), + 'handler_with' => [ + 'host' => env('PAPERTRAIL_URL'), + 'port' => env('PAPERTRAIL_PORT'), + 'connectionString' => 'tls://' . env('PAPERTRAIL_URL') . ':' . env('PAPERTRAIL_PORT'), + ], + 'processors' => [PsrLogMessageProcessor::class], + ], + + 'stderr' => [ + 'driver' => 'monolog', + 'level' => env('LOG_LEVEL', 'debug'), + 'handler' => StreamHandler::class, + 'formatter' => env('LOG_STDERR_FORMATTER'), + 'with' => [ + 'stream' => 'php://stderr', + ], + 'processors' => [PsrLogMessageProcessor::class], + ], + + 'syslog' => [ + 'driver' => 'syslog', + 'level' => env('LOG_LEVEL', 'debug'), + 'facility' => env('LOG_SYSLOG_FACILITY', LOG_USER), + 'replace_placeholders' => true, + ], + + 'errorlog' => [ + 'driver' => 'errorlog', + 'level' => env('LOG_LEVEL', 'debug'), + 'replace_placeholders' => true, + ], + + 'null' => [ + 'driver' => 'monolog', + 'handler' => NullHandler::class, + ], + + 'emergency' => [ + 'path' => storage_path('logs/laravel.log'), + ], + + ], + +];