Skip to content

Commit

Permalink
Print exception data to console output (#118)
Browse files Browse the repository at this point in the history
* print exception data to console output
* add exception data to the General log
* fix logging config
  • Loading branch information
andrew-nuwber authored Aug 14, 2023
1 parent 1c99041 commit 4b433b0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 36 deletions.
19 changes: 8 additions & 11 deletions src/RabbitEvents/Listener/Commands/ListenCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public function handle(Context $context, Worker $worker)
{
$options = $this->gatherProcessingOptions();

$this->registerLogWriters($options->connectionName);

$this->registerLogWriters();
$this->listenForEvents();

$queue = $context->makeQueue(
Expand Down Expand Up @@ -123,30 +122,28 @@ protected function listenForEvents(): void

/**
* Register classes to write log output
*
* @param string $connectionName
*/
protected function registerLogWriters(string $connectionName): void
protected function registerLogWriters(): void
{
if (!$this->option('quiet')) {
$this->logWriters[] = new Log\Output($this->laravel, $this->output);
}

[$enabled, $defaultLoglevel, $channel] = $this->parseLoggingConfiguration($connectionName);
[$enabled, $defaultLoglevel, $channel] = $this->parseLoggingConfiguration();

if ($enabled) {
$this->logWriters[] = new Log\General($this->laravel, $defaultLoglevel, $channel);
}
}

private function parseLoggingConfiguration(string $connectionName): array
private function parseLoggingConfiguration(): array
{
$config = $this->laravel['config']->get('rabbitevents.connections');
$config = $this->laravel['config']->get('rabbitevents');

return [
Arr::get($config, "$connectionName.logging.enabled", false),
Arr::get($config, "$connectionName.logging.level", 'info'),
Arr::get($config, "$connectionName.logging.channel"),
Arr::get($config, 'logging.enabled', false),
Arr::get($config, 'logging.level', 'info'),
Arr::get($config, 'logging.channel'),
];
}

Expand Down
36 changes: 21 additions & 15 deletions src/RabbitEvents/Listener/Commands/Log/General.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace RabbitEvents\Listener\Commands\Log;

use Illuminate\Contracts\Container\Container;
use RabbitEvents\Listener\Message\Handler;
use RabbitEvents\Listener\Events\ListenerHandlerExceptionOccurred;

class General extends Writer
{
Expand All @@ -23,25 +23,31 @@ public function log($event): void
{
$status = $this->getStatus($event);

$this->write($event->handler, $status);
}

protected function write(Handler $handler, string $status): void
{
$this->app['log']->channel($this->channel)->log(
$this->getLogLevel($status),
sprintf('Handler "%s" %s', $handler->getName(), $status),
[
'handler' => [
'name' => $handler->getName(),
'attempts' => $handler->attempts(),
'payload' => $handler->payload(),
],
'status' => $status,
]
sprintf('Handler "%s" %s', $event->handler->getName(), $status),
$this->getPayload($event, $status),
);
}

protected function getPayload($event, string $status): array
{
$payload = [
'handler' => [
'name' => $event->handler->getName(),
'attempts' => $event->handler->attempts(),
'payload' => $event->handler->payload(),
],
'status' => $status,
];

if ($event instanceof ListenerHandlerExceptionOccurred) {
$payload['exception'] = $event->exception;
}

return $payload;
}

protected function getLogLevel(string $status): string
{
if (in_array($status, [static::STATUS_EXCEPTION, static::STATUS_FAILED])) {
Expand Down
7 changes: 5 additions & 2 deletions src/RabbitEvents/Listener/Commands/Log/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use Illuminate\Console\OutputStyle;
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\Carbon;
use RabbitEvents\Listener\Events\ListenerHandlerExceptionOccurred;
use RabbitEvents\Listener\Message\Handler;
use Symfony\Component\Console\Application as ConsoleApplication;

class Output extends Writer
{
Expand All @@ -23,8 +25,9 @@ public function log($event): void
$status = $this->getStatus($event);

$this->writeStatus($event->handler, $status, $this->getType($status));
if (isset($event->exception)) {
$this->output->writeln('Exception message: ' . $event->exception->getMessage());

if ($event instanceof ListenerHandlerExceptionOccurred) {
(new ConsoleApplication())->renderThrowable($event->exception, $this->output);
}
}

Expand Down
22 changes: 14 additions & 8 deletions src/RabbitEvents/Listener/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ If you need just to handle events, you could use the RabbitEvents `Listener` sep
1. [Middleware](#listener-middleware)
1. [Stopping The Propagation Of An Event](#stopping-propagination)
1. [Console commands](#commands)
1. [Logging](#ligging)
1. [Logging](#logging)

## Installation via Composer<a name="installation"></a>
RabbitEvents Listener may be installed via the Composer package manager:
Expand Down Expand Up @@ -203,12 +203,16 @@ You could start listening to an event only by using `rabbitevents:listen` comman
If your listener crashes, then managers will rerun your listener and all messages sent to a queue will be handled in the same order as they were sent. There is the known problem: as queues are separated and you have messages that affect the same entity there's no guarantee that all actions will be done in an expected order. To avoid such problems you can send message time as a part of the payload and handle it internally in your listeners.

### Options<a name="listen-options"></a>
- **--service=**. When a queue starts the name of the service becomes a part of a queue name: `service:event.name`. By default, service is the APP_NAME from your `.env`. You could override the first part of a queue name by this option.
- **--memory=128**. The memory limit in megabytes. The RabbitEvents have restarting a worker if limit exceeded.
- **--timeout=60**. The length of time (in seconds) each Message should be allowed to be handled.
- **--tries=1**. Number of times to attempt to handle a Message before logging it failed.
- **--sleep=5**. Sleep time in seconds before handling failed message next time.
- **--quiet**. No console output

| Option | Default value | Description |
|--------------|--------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `--service=` | result of `config('app.name')` | When a queue starts the name of the service becomes a part of a queue name: `service:event.name`. You could override the first part of a queue name by this option. |
| `--memory=` | `128` | The memory limit in megabytes. The RabbitEvents have restarting a worker if limit exceeded. |
| `--timeout=` | `60` | The length of time (in seconds) each Message should be allowed to be handled. |
| `--tries=` | `1` | Number of times to attempt to handle a Message before logging it failed. |
| `--sleep=` | `5` | Sleep time in seconds before handling failed message next time. |
| `--quiet` | disabled | No console output. |
| `-v` | disabled | Verbosity. Enables stack trace for exceptions. |

## Command `rabbitevents:list` <a name='command-list'></a>

Expand All @@ -223,7 +227,9 @@ The suprvisor configuration is similar to [Laravel Queue](https://laravel.com/do

# Logging <a name='logging'></a>

The package provides 2 ways to see what happens to your listener. By default, it writes `processing`, `processed`, and `failed` messages to `/php/stdout`. The message includes service, event, and listener name. If you want to turn this feature off, just run listener with the `--quiet` option.
The package provides 2 ways to see what happens to your listener. By default, it writes `processing`, `processed`, `failed` messages and occurred exceptions to console output.
The message includes service, event, and listener name. To get exception's trace run listener with verbosity >= 1, for example `-v`.
If you want to turn this feature off, just run listener with the `--quiet` option.

The package also supports your application logger. To use it set config value `rabbitevents.logging.enabled` to `true` and choose log level.

Expand Down

0 comments on commit 4b433b0

Please sign in to comment.