From a0e408a7582b22dad2caacf7ecc4b6d2ee6a4154 Mon Sep 17 00:00:00 2001 From: "Ing. Luca Stucchi" Date: Thu, 3 Dec 2020 12:23:47 +0100 Subject: [PATCH] Implementation of a template engine for the log entry based on blade templates --- README.md | 16 ++++++++++++++ config/telegram-logger.php | 5 ++++- src/TelegramHandler.php | 32 ++++++++++++++++++++++----- src/TelegramLoggerServiceProvider.php | 1 + src/views/minimal.blade.php | 2 ++ src/views/standard.blade.php | 3 +++ 6 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 src/views/minimal.blade.php create mode 100644 src/views/standard.blade.php diff --git a/README.md b/README.md index 35c153e..db29b1a 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,22 @@ Publish config file php artisan vendor:publish --provider "Logger\TelegramLoggerServiceProvider" ``` +## Telegram Logging Formats + +In version 1.3.3 a new Logging Format Engine based on blade templates has been introduced. +While off the shelf the behavior is exactly the same as before, you can choose among two different formats +that you can specify in the `.env` file like this : + +``` +# Use a minimal log template +TELEGRAM_LOGGER_TEMPLATE = laravel-telegram-logging::minimal + +# Or use the backward compatible one (default setting used even without inserting this row) +TELEGRAM_LOGGER_TEMPLATE = laravel-telegram-logging::standard +``` + +It is possible to create other blade templates and reference them in the `TELEGRAM_LOGGER_TEMPLATE` entry + ## Create bot For using this package you need to create Telegram bot diff --git a/config/telegram-logger.php b/config/telegram-logger.php index 9838fab..0ae7a23 100644 --- a/config/telegram-logger.php +++ b/config/telegram-logger.php @@ -5,5 +5,8 @@ 'token' => env('TELEGRAM_LOGGER_BOT_TOKEN'), // Telegram chat id - 'chat_id' => env('TELEGRAM_LOGGER_CHAT_ID') + 'chat_id' => env('TELEGRAM_LOGGER_CHAT_ID'), + + // Blade Template to use formatting logs + 'template' => env('TELEGRAM_LOGGER_TEMPLATE', 'laravel-telegram-logging::standard') ]; \ No newline at end of file diff --git a/src/TelegramHandler.php b/src/TelegramHandler.php index d1e8018..d551880 100644 --- a/src/TelegramHandler.php +++ b/src/TelegramHandler.php @@ -3,6 +3,8 @@ namespace Logger; use Exception; +use Monolog\Formatter\FormatterInterface; +use Monolog\Formatter\LineFormatter; use Monolog\Logger; use Monolog\Handler\AbstractProcessingHandler; @@ -40,6 +42,13 @@ class TelegramHandler extends AbstractProcessingHandler */ private $appEnv; + /** + * Blade template reference to be used by Logs + * + * @string + */ + private $template; + /** * TelegramHandler constructor. * @param int $level @@ -53,6 +62,7 @@ public function __construct($level) // define variables for making Telegram request $this->botToken = config('telegram-logger.token'); $this->chatId = config('telegram-logger.chat_id'); + $this->template = config('telegram-logger.template'); // define variables for text message $this->appName = config('app.name'); @@ -73,7 +83,7 @@ public function write(array $record): void file_get_contents( 'https://api.telegram.org/bot' . $this->botToken . '/sendMessage?' . http_build_query([ - 'text' => $this->formatText($record['formatted'], $record['level_name']), + 'text' => $this->formatText($record), 'chat_id' => $this->chatId, 'parse_mode' => 'html' ]) @@ -84,12 +94,24 @@ public function write(array $record): void } /** - * @param string $text - * @param string $level + * {@inheritDoc} + */ + protected function getDefaultFormatter(): FormatterInterface + { + return new LineFormatter("%message% %context% %extra%\n"); + } + + /** + * @param array $record * @return string */ - private function formatText(string $text, string $level): string + private function formatText(array $record): string { - return '' . $this->appName . ' (' . $level . ')' . PHP_EOL . 'Env: ' . $this->appEnv . PHP_EOL . $text; + + return view($this->template, array_merge($record,[ + 'appName' => $this->appName, + 'appEnv' => $this->appEnv, + ]) + ); } } \ No newline at end of file diff --git a/src/TelegramLoggerServiceProvider.php b/src/TelegramLoggerServiceProvider.php index c876867..cb992f6 100644 --- a/src/TelegramLoggerServiceProvider.php +++ b/src/TelegramLoggerServiceProvider.php @@ -27,6 +27,7 @@ public function register() */ public function boot() { + $this->loadViewsFrom(__DIR__.'/views', 'laravel-telegram-logging'); $this->publishes([__DIR__ . '/../config/telegram-logger.php' => config_path('telegram-logger.php')], 'config'); } } \ No newline at end of file diff --git a/src/views/minimal.blade.php b/src/views/minimal.blade.php new file mode 100644 index 0000000..48fd499 --- /dev/null +++ b/src/views/minimal.blade.php @@ -0,0 +1,2 @@ +{{ $appName }} ({{ $level_name }}) +{{ $formatted }} \ No newline at end of file diff --git a/src/views/standard.blade.php b/src/views/standard.blade.php new file mode 100644 index 0000000..855c4c2 --- /dev/null +++ b/src/views/standard.blade.php @@ -0,0 +1,3 @@ +{{ $appName }} ({{ $level_name }}) +Env: {{ $appEnv }} +[{{ $datetime->format('Y-m-d H:i:s') }}] {{ $appEnv }}.{{ $level_name }} {{ $formatted }} \ No newline at end of file