4

I want to send logs directly to slack channell. Service provider that I make for Laravel projects works great but when I add provider to Lumen I got error:

Call to undefined method Monolog\Logger::getMonolog()

This is boot method in my Provider:

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\SlackWebhookHandler;
use Monolog\Logger;

class SendLogsToSlackServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        $monolog      = Log::getMonolog();

        $slackHandler = new SlackWebhookHandler(
            env('SLACK_WEBHOOK'), 
            env('SLACK_USER'), 
            env('SLACK_BOT_NAME'),
            $useAttachment = true, 
            $iconEmoji = null, 
            $useShortAttachment = false, 
            $includeContextAndExtra = true,
            $level = Logger::NOTICE, 
            $bubble = true
        );

        $monolog->pushHandler($slackHandler);
        $slackHandler->setFormatter(new LineFormatter());
    }
}
nicponim
  • 41
  • 1
  • 4
  • Did you add `use Illuminate\Support\Facades\Log;` to the top of your script? – milo526 Feb 01 '18 at 17:43
  • yes i did this, checkout my update on question i paste whole Provider. – nicponim Feb 01 '18 at 17:48
  • in your `config/app.php` what is located in the facades array behing log? – milo526 Feb 01 '18 at 18:06
  • @milo526 unfortunately there is no `config/app.php` its Lumen. In ` bootstrap / app.php` i register provider with `$app->register(App\Providers\SendLogsToSlackServiceProvider::class);` – nicponim Feb 02 '18 at 17:15

1 Answers1

2

This error occurs when you're using the new version of the Monolog/Monolog with the previous api interface .... just update your method with the available methods in the vendor\monolog\monolog\src\Monolog\Logger.php file

Mohamad Pishdad
  • 329
  • 3
  • 11
  • 2
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](https://stackoverflow.com/review/low-quality-posts/22266669) – Bruce Mar 09 '19 at 10:51
  • This worked for me, i went to the Logger.php and found the latest one has different functions for example to log info the function is info(), i was using addInfo(), which is for the older package. – silvedo Apr 29 '21 at 08:49