Skip to content

Commit

Permalink
Implementation of a template engine for the log entry based on blade …
Browse files Browse the repository at this point in the history
…templates
  • Loading branch information
stucchi committed Dec 3, 2020
1 parent f42529e commit a0e408a
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 6 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion config/telegram-logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
];
32 changes: 27 additions & 5 deletions src/TelegramHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Logger;

use Exception;
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\LineFormatter;
use Monolog\Logger;
use Monolog\Handler\AbstractProcessingHandler;

Expand Down Expand Up @@ -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
Expand All @@ -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');
Expand All @@ -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'
])
Expand All @@ -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 '<b>' . $this->appName . '</b> (' . $level . ')' . PHP_EOL . 'Env: ' . $this->appEnv . PHP_EOL . $text;

return view($this->template, array_merge($record,[
'appName' => $this->appName,
'appEnv' => $this->appEnv,
])
);
}
}
1 change: 1 addition & 0 deletions src/TelegramLoggerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
2 changes: 2 additions & 0 deletions src/views/minimal.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<b>{{ $appName }}</b> ({{ $level_name }})
{{ $formatted }}
3 changes: 3 additions & 0 deletions src/views/standard.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<b>{{ $appName }}</b> ({{ $level_name }})
Env: {{ $appEnv }}
[{{ $datetime->format('Y-m-d H:i:s') }}] {{ $appEnv }}.{{ $level_name }} {{ $formatted }}

0 comments on commit a0e408a

Please sign in to comment.