Skip to content

Commit a0e408a

Browse files
committed
Implementation of a template engine for the log entry based on blade templates
1 parent f42529e commit a0e408a

File tree

6 files changed

+53
-6
lines changed

6 files changed

+53
-6
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,22 @@ Publish config file
4747
php artisan vendor:publish --provider "Logger\TelegramLoggerServiceProvider"
4848
```
4949

50+
## Telegram Logging Formats
51+
52+
In version 1.3.3 a new Logging Format Engine based on blade templates has been introduced.
53+
While off the shelf the behavior is exactly the same as before, you can choose among two different formats
54+
that you can specify in the `.env` file like this :
55+
56+
```
57+
# Use a minimal log template
58+
TELEGRAM_LOGGER_TEMPLATE = laravel-telegram-logging::minimal
59+
60+
# Or use the backward compatible one (default setting used even without inserting this row)
61+
TELEGRAM_LOGGER_TEMPLATE = laravel-telegram-logging::standard
62+
```
63+
64+
It is possible to create other blade templates and reference them in the `TELEGRAM_LOGGER_TEMPLATE` entry
65+
5066
## Create bot
5167

5268
For using this package you need to create Telegram bot

config/telegram-logger.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@
55
'token' => env('TELEGRAM_LOGGER_BOT_TOKEN'),
66

77
// Telegram chat id
8-
'chat_id' => env('TELEGRAM_LOGGER_CHAT_ID')
8+
'chat_id' => env('TELEGRAM_LOGGER_CHAT_ID'),
9+
10+
// Blade Template to use formatting logs
11+
'template' => env('TELEGRAM_LOGGER_TEMPLATE', 'laravel-telegram-logging::standard')
912
];

src/TelegramHandler.php

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Logger;
44

55
use Exception;
6+
use Monolog\Formatter\FormatterInterface;
7+
use Monolog\Formatter\LineFormatter;
68
use Monolog\Logger;
79
use Monolog\Handler\AbstractProcessingHandler;
810

@@ -40,6 +42,13 @@ class TelegramHandler extends AbstractProcessingHandler
4042
*/
4143
private $appEnv;
4244

45+
/**
46+
* Blade template reference to be used by Logs
47+
*
48+
* @string
49+
*/
50+
private $template;
51+
4352
/**
4453
* TelegramHandler constructor.
4554
* @param int $level
@@ -53,6 +62,7 @@ public function __construct($level)
5362
// define variables for making Telegram request
5463
$this->botToken = config('telegram-logger.token');
5564
$this->chatId = config('telegram-logger.chat_id');
65+
$this->template = config('telegram-logger.template');
5666

5767
// define variables for text message
5868
$this->appName = config('app.name');
@@ -73,7 +83,7 @@ public function write(array $record): void
7383
file_get_contents(
7484
'https://api.telegram.org/bot' . $this->botToken . '/sendMessage?'
7585
. http_build_query([
76-
'text' => $this->formatText($record['formatted'], $record['level_name']),
86+
'text' => $this->formatText($record),
7787
'chat_id' => $this->chatId,
7888
'parse_mode' => 'html'
7989
])
@@ -84,12 +94,24 @@ public function write(array $record): void
8494
}
8595

8696
/**
87-
* @param string $text
88-
* @param string $level
97+
* {@inheritDoc}
98+
*/
99+
protected function getDefaultFormatter(): FormatterInterface
100+
{
101+
return new LineFormatter("%message% %context% %extra%\n");
102+
}
103+
104+
/**
105+
* @param array $record
89106
* @return string
90107
*/
91-
private function formatText(string $text, string $level): string
108+
private function formatText(array $record): string
92109
{
93-
return '<b>' . $this->appName . '</b> (' . $level . ')' . PHP_EOL . 'Env: ' . $this->appEnv . PHP_EOL . $text;
110+
111+
return view($this->template, array_merge($record,[
112+
'appName' => $this->appName,
113+
'appEnv' => $this->appEnv,
114+
])
115+
);
94116
}
95117
}

src/TelegramLoggerServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function register()
2727
*/
2828
public function boot()
2929
{
30+
$this->loadViewsFrom(__DIR__.'/views', 'laravel-telegram-logging');
3031
$this->publishes([__DIR__ . '/../config/telegram-logger.php' => config_path('telegram-logger.php')], 'config');
3132
}
3233
}

src/views/minimal.blade.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<b>{{ $appName }}</b> ({{ $level_name }})
2+
{{ $formatted }}

src/views/standard.blade.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<b>{{ $appName }}</b> ({{ $level_name }})
2+
Env: {{ $appEnv }}
3+
[{{ $datetime->format('Y-m-d H:i:s') }}] {{ $appEnv }}.{{ $level_name }} {{ $formatted }}

0 commit comments

Comments
 (0)