Skip to content

Commit

Permalink
finnish
Browse files Browse the repository at this point in the history
  • Loading branch information
ExpDev07 committed Mar 25, 2022
1 parent cd967a9 commit e46a2eb
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 10 deletions.
35 changes: 33 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
# laravel-logsnag

[![Latest Version on Packagist](https://img.shields.io/packagist/v/expdev07/laravel-logsnag.svg?style=flat-square)](https://packagist.org/packages/expdev07/laravel-logsnag)
[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/expdev07/laravel-logsnag/run-tests?label=tests)](https://github.com/expdev07/laravel-logsnag/actions?query=workflow%3Arun-tests+branch%3Amain)
[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/expdev07/laravel-logsnag/Check%20&%20fix%20styling?label=code%20style)](https://github.com/expdev07/laravel-logsnag/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/expdev07/laravel-logsnag.svg?style=flat-square)](https://packagist.org/packages/expdev07/laravel-logsnag)

Get a realtime feed of your Laravel project’s most important events using Logsnag. Supports push notifications straight to your
Expand Down Expand Up @@ -48,10 +46,30 @@ return [
|
*/

/**
* The project name.
*/
'project' => env('LOGSNAG_PROJECT', 'laravel'),

/**
* The API token.
*/
'token' => env('LOGSNAG_TOKEN', ''),

/**
* A mapping of icons for logging.
*/
'icons' => [
'DEBUG' => 'ℹ️',
'INFO' => 'ℹ️',
'NOTICE' => '📌',
'WARNING' => '⚠️',
'ERROR' => '⚠️',
'CRITICAL' => '🔥',
'ALERT' => '🔔️',
'EMERGENCY' => '💀',
],

];
```

Expand All @@ -63,8 +81,10 @@ Add the Logsnag channel to your logging config:
'logsnag' => [
'driver' => 'custom',
'via' => ExpDev07\Logsnag\Logger\LogsnagLogger::class,
'level' => 'debug',
'project' => 'my-project',
'channel' => 'my-channel',
'notify' => true,
],
];
```
Expand Down Expand Up @@ -107,6 +127,17 @@ app(LogsnagClient::class)->log(new LogsnagRequest(
));
```

## Parameters

* **project:** The logsnag project name.
* **channel:** The channel to log in. Must be lowercase and hyphenated.
* **event:** The event name.
* **description:** The event description.
* **icon:** Associate the log with an icon (emoji).
* **notify:** Whether to send push notifications to devices.

See [Logsnag Log](https://sh4yy.notion.site/LogSnag-API-e942b03305c94d4fa72c8a3d24a0ad49#eb98c978cec841d0ab50d52be6eb9f80) route for more information.

## Testing

```bash
Expand Down
20 changes: 20 additions & 0 deletions config/logsnag.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,28 @@
|
*/

/**
* The project name.
*/
'project' => env('LOGSNAG_PROJECT', 'laravel'),

/**
* The API token.
*/
'token' => env('LOGSNAG_TOKEN', ''),

/**
* A mapping of icons for logging.
*/
'icons' => [
'DEBUG' => 'ℹ️',
'INFO' => 'ℹ️',
'NOTICE' => '📌',
'WARNING' => '⚠️',
'ERROR' => '⚠️',
'CRITICAL' => '🔥',
'ALERT' => '🔔️',
'EMERGENCY' => '💀',
],

];
3 changes: 3 additions & 0 deletions src/Client/LogsnagRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Illuminate\Contracts\Support\Arrayable;

/**
* A Logsnag logging request.
*/
class LogsnagRequest implements Arrayable
{

Expand Down
2 changes: 1 addition & 1 deletion src/Facades/Logsnag.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @see \ExpDev07\Logsnag\Logsnag
*
* @method static void log(string $channel, string $event)
* @method static void log(string $channel, string $event, string $description = null, string $icon = null, bool $notify = false)
*/
class Logsnag extends Facade
{
Expand Down
48 changes: 44 additions & 4 deletions src/Logger/LogsnagHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace ExpDev07\Logsnag\Logger;

use ExpDev07\Logsnag\Facades\Logsnag;
use Illuminate\Support\Str;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;

/**
*
* The Monolog processing handler.
*/
class LogsnagHandler extends AbstractProcessingHandler
{
Expand All @@ -18,16 +20,26 @@ class LogsnagHandler extends AbstractProcessingHandler
*/
public string $channel;

/**
* Whether to send push notification.
*
* @var string
*/
public string $notify;

/**
* Constructs a new monolog Logsnag handler.
*
* @param string $channel
* @param bool $notify
* @param int $level
*/
public function __construct(string $channel)
public function __construct(string $channel, bool $notify = false, int $level = Logger::DEBUG)
{
parent::__construct();
parent::__construct($level);

$this->channel = $channel;
$this->notify = $notify;
}

/**
Expand All @@ -37,7 +49,35 @@ public function __construct(string $channel)
*/
protected function write(array $record): void
{
Logsnag::log($this->channel, $record['formatted']);
Logsnag::log($this->channel,
event: $this->getEvent($record),
icon: $this->getIcon($record),
notify: $this->notify,
);
}

/**
* Gets the event for the record.
*
* @param array $record
* @return string
*/
public function getEvent(array $record): string
{
return $record['formatted'];
}

/**
* Gets the icon for the record.
*
* @param array $record
* @return string|null
*/
protected function getIcon(array $record): ?string
{
$icons = config('logsnag.icons');

return $icons[Str::upper($record['level_name'])] ?? null;
}

}
14 changes: 11 additions & 3 deletions src/Logger/LogsnagLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace ExpDev07\Logsnag\Logger;

use Arr;
use Illuminate\Support\Arr;
use Monolog\Logger;

/**
* The Laravel Logsnag logger.
*/
class LogsnagLogger
{

Expand All @@ -16,9 +19,14 @@ class LogsnagLogger
*/
public function __invoke(array $config): Logger
{
[$channel] = $config;
// Create handler.
$handler = new LogsnagHandler(
channel: $config['channel'],
notify: $config['notify'] ?? false,
level: Logger::toMonologLevel($config['level'] ?? 'debug'),
);

return new Logger('logsnag', Arr::wrap(new LogsnagHandler($channel)));
return new Logger('logsnag', Arr::wrap($handler));
}

}
3 changes: 3 additions & 0 deletions src/Logsnag.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use ExpDev07\Logsnag\Client\LogsnagClient;
use ExpDev07\Logsnag\Client\LogsnagRequest;

/**
* The Logsnag logger.
*/
class Logsnag
{

Expand Down
3 changes: 3 additions & 0 deletions src/LogsnagServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

/**
* The Logsnag service provider.
*/
class LogsnagServiceProvider extends PackageServiceProvider
{

Expand Down

0 comments on commit e46a2eb

Please sign in to comment.