Playing
05. The log() helper

Transcript

00:00
Laravel has introduced a new log helper, which makes it a little bit cleaner to log stuff out, either to your log file or if you're trailing logs with something like Laravel Pale. Let's go ahead and have a look.
00:12
So the first thing that I'm going to do is do a composer require on Laravel Pale. This will allow us to go ahead and run php artisan pale to trail our log. So if we run this, we'll just hang around in here. And if we go ahead and use the standard way of pulling in our logo within Laravel
00:30
and we log something out, we go ahead and hit this route. Sure enough, that gets locked just over here. And of course, you'll also find that over in your Laravel log file as well. OK, so this new helper that's introduced is literally the log function.
00:45
However, the log function is actually a PHP function that deals with logarithms. So how does this work? Well, what we can actually do is go ahead and pull this in from illuminate and log. And this is a function that's registered under here.
01:00
So let's go ahead and pull this in. And we can go ahead and replace this out with an alias as well if we wanted to. And to be honest, we could even just get rid of the alias. So let's go ahead and just call this log.
01:12
And we'll go ahead and get rid of that alias from there. Or you could alias it to something else if you wanted to, like log this, for example. You probably wouldn't do that. Now we have access to this log function, which works in exactly the same way.
01:24
So we'll say logged from log. And let's head over, give that a refresh, come back over. And you can see that this works in exactly the same way. So although it doesn't have a specific type of log, so this was an info log,
01:38
this is just a general debug. But we can still pass through to this if we actually look at the definition of this, a context in here as well. So if you needed to log out some context to either the console or to your log file,
01:51
you can pass anything you need in here. So as an example, let's go ahead and log out to one of the users that I have in this application. So we're just going to find a user here, cast that to an array, and let's go over and give that a refresh.
02:05
And yeah, let's just make sure we pull the namespace in for user here quickly. And let's go back over and log this. And there we go. We get a bunch of the context in here, of course, as well to allow a log file as well.
02:17
If we just find this by running it again, there we go. So we've got all of the context directly in here as well. So a really simple change, but some people might prefer this. It's a lot cleaner, particularly if you're doing a lot of logging.
02:29
It's a lot easier to access while you are doing a bunch of logging than using log and then info by choosing the type of log that you want to run. So there we go. That is the new log function helper in Laravel.
26 episodes2 hrs 34 mins

Overview

Need to know what’s new in Laravel as it happens? Every episode of this course is dedicated to covering the most interesting and useful Laravel additions in detail, so you’re ready to start using them in your applications.

Check back often, and stay completely up-to-date with Laravel.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!