1

In Laravel 5.6 I'm trying to make proper slack logs and I did:

'channels' => [
    'stack' => [
        'driver' => 'stack',
        'channels' => ['single', 'slack'],
    ],

    'slack' => [
        'driver' => 'slack',
        'url' => env('LOG_SLACK_WEBHOOK_URL'),
        'username' => 'TEST',
        'icon' => ':boom:',
        'level' => 'info',
    ],

It works but I want to specify additional fields and maybe customize it a little if it match some other conditions.

I was looking at SlackWebhookHandler.php monolog file but not all parameters work in this configuration.. For example emoji and username doesn't work - I don't know if slack already has even options for changing bot username. Other example is that in this file something it's called useAttachment and here it's just attachment - where the names are stored..?

Back to topic I did:

 Log::info('added test',['test'=>'test']);

And it works, but for slack I want to send additional field, in every request for example:

'added test',['test'=>'test', 'more' => 'test2']

How I'm able to accomplish it? I need to connect to Log Class and slack driver in some way but I don't have idea how to do this?

Davidos
  • 419
  • 5
  • 17

2 Answers2

1

I debugged myself to SlackRecord::getSlackData, there you see how he handles attachments and add's additional data to the record.

For me it totally fitted to set 'context' => true in logging.php for the Slack Channel and define a Processor which just add's the Data I need to the record

class SlackProcessor {

    /**
     * @param array $record
     * @return array
     */
    public function __invoke(array $record) {
        $record["context"]["Env"] = env("LOG_SLACK_USERNAME", "localhost");
        $record["context"]["Full URL"] = Request::fullUrl();
        $record["extra"]["Request Data"] = Request::all();

        return $record;
    }
}

So maybe you could just debug again to getSlackData and see why he jumps over the attachment part you need.

0

I was able to get closer to solution but still not at all: On logging.php now I have

'slack' => [
    'driver' => 'slack',
    'url' => env('LOG_SLACK_WEBHOOK_URL'),
    'tap' => [App\Logging\SlackLogger::class],
    'username' => 'BOT',
    'attachment' => false,
    'emoji' => ':boom:',
    'level' => 'info',
],

I created App/Logging/SlackLogger.php:

namespace App\Logging;

use Monolog\Logger;
use Monolog\Handler\SlackWebhookHandler;
use Monolog\Formatter\LineFormatter;
use Monolog\Formatter\JsonFormatter;


class SlackLogger
{
    /**
     * Customize the given logger instance.
     *
     * @param  \Illuminate\Log\Logger  $logger
     * @return void
     */
    public function __invoke($logger)
    {
        $dateFormat = "Y-m-d H:i:s";
        $checkLocal = env('APP_ENV');

        foreach ($logger->getHandlers() as $handler) {
            if ($handler instanceof SlackWebhookHandler) {
                $output = "[$checkLocal]: %datetime% > %level_name% - %message% `%context% %extra%` :poop: \n";
                $formatter = new LineFormatter($output, $dateFormat);

                $handler->setFormatter($formatter);
                $handler->pushProcessor(function ($record) {

                    $record['extra']['dummy'] = 'test';

                    return $record;
                });
            }
        }
    }

}

And It works only if I don't try to make custom attachment on slack.. When I'm trying to do:

$handler->pushProcessor(function ($record) {

  $record['extra']['dummy'] = 'test';

  $record['attachments'] = [
      'color' => "#36a64f",
      "title" => "Slack API Documentation",
      "text" => "Optional text that appears within the attachment"
  ];

  return $record;
});

the $record losts 'attachments' array.. I was checking it in SlackWebhookHandler in write function because at this pushProcessor at return it still exists, but not sending to slack. I know that can be related to $handler->setFormatter($formatter); but I if I remove It, the problem still exists - so I still don't know how to solve it.

Davidos
  • 419
  • 5
  • 17