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