Is there a way to kind of dd() on an Event Listener?

Published 9 months ago by splendidkeen

Hey, How can I search for errors/problems within an Event Listener setup?

Best Answer (As Selected By splendidkeen)
Snapey

At the top of the class use Log;

inline in your code;

Log::info('hey something just happened');

Log::info($myevent);

etc

Talinon

Sort of... One way I can think of is to run the artisan queue worker from the command line without forking it into the background. The dd() will output to your terminal window:

php artisan queue:work

Snapey
Snapey
9 months ago (1,038,085 XP)

Write to the Laravel logfile ?

splendidkeen

How would that look like? I have not done this before. I am struggling with a Event Listener incl. a polymorphic relation, but how could I test if something is happening?

Snapey
Snapey
9 months ago (1,038,085 XP)

At the top of the class use Log;

inline in your code;

Log::info('hey something just happened');

Log::info($myevent);

etc

splendidkeen

Thank you @Snapey

With class you mean within the Listeners file?

Snapey
Snapey
9 months ago (1,038,085 XP)

With class you mean within the Listeners file?

wherever you want to log.

Talinon

For me, the output is quite similar to the browser. Colour coded and fully expanded. I'm using a Linux system - not sure how it would be formatted on other platforms.

splendidkeen

That would be within my AwardPointsForFollowing (Listener)

use Log;
....


 public function handle(FollowedPartner $event)
{
      
        $event->follow->user->awardPoints($event, 5, $event->follow);
    Log::info('hey something just happened');
        Log::info($event);
}

@snapey Wow this file is huge, but I am not seeing a related log to this Listener

Snapey
Snapey
9 months ago (1,038,085 XP)

If you have not already setup daily log rotation, just delete the file.

splendidkeen

Delete laravel.log? @snapey

Snapey
Snapey
9 months ago (1,038,085 XP)

yes

splendidkeen

I tried

public function follow(Partner $partner, User $user, FollowedPartner $event){

        Log::info('hey something just happened');
                Log::info($event);

        Auth::user()->followPartner($partner);
        
        return redirect()->route('partner.show', $partner)->with('info', 'Followed.');

which results in (error)

Object of class App\Events\FollowedPartner could not be converted to string
in Logger.php

@Snapey

Maybe that is why it won't work, because within the setup I need the event class as a string


        public function awardPoints($event, $points, User $user){   
        $adjustment = new PointAdjustment([
                'event' => get_class($event),
                'points' => $points,
            ]);
    
            $adjustment->user()->associate($user);
    
            return $this-> pointsAdjustments()->save($adjustment);
        }
Talinon
Log::info(print_r($event, true));
Snapey
Snapey
9 months ago (1,038,085 XP)

well what I gave you was just an example of how to use Log

you can log whatever you want, just a string or elements of an object

dumping the whole object is perhaps a bit much

splendidkeen

Still results in

Object of class App\Events\FollowedPartner could not be converted to string
in Logger.php

Please sign in or create an account to participate in this conversation.