khoanguyenme's avatar

L5 Monolog with Slack integration

I'm using Monolog with ChromePHP and now I want to integrate it with Slack but I can't config SlackHandler to works. In app\Providers\AppServiceProviders.php:

<?php namespace Quiz\Providers;

use Illuminate\Support\ServiceProvider;
use Log;

class AppServiceProvider extends ServiceProvider {

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Log::listen(function()
        {
            $monolog = Log::getMonolog();

            if (env('APP_ENV') === 'local')
            {
                $monolog->pushHandler($chromeHandler = new \Monolog\Handler\ChromePHPHandler());
                $chromeHandler->setFormatter(new \Monolog\Formatter\ChromePHPFormatter());
            }

            $slackHandler = new \Monolog\Handler\SlackHandler(
                'my-api-token',
                'random'
            );
            $monolog->pushHandler($slackHandler);
            $slackHandler->setFormatter(new \Monolog\Formatter\LineFormatter());

        });
}

I get my-api-token at https://api.slack.com/web#auth Try to log somethings but only ChromePHP (and of course default Laravel file log works).

Is there anything wrong with my config? I also open a issue on Monolog's github but no response yet. https://github.com/Seldaek/monolog/issues/514

0 likes
5 replies
onbjerg's avatar

Do you have openssl installed on your server/machine?

khoanguyenme's avatar

@onbjerg MonoLog SlackHandler have to check to throw OpenSSL error. but yes, I installed OpenSSL.

echo OPENSSL_VERSION_NUMBER;
==> 268439711
smit2482's avatar

For anyone visiting this thread: I solved a similar issue by adding the hashtag before the channel name.

 $slackHandler = new \Monolog\Handler\SlackHandler(
    'my-api-token',
    '#random'
);

(cf. the "Channels" section here: https://api.slack.com/methods/chat.postMessage )

Also another thing to consider is the minimum logging $level of each handler. (e.g. The SlackHandler by default only logs CRITICAL and above.)

2 likes

Please or to participate in this conversation.