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.

Episode summary

In this episode, we're taking a look at a handy new feature in Laravel: the log() helper function! We start by setting up Laravel Pale using Composer so we can trail our logs in real time with php artisan pale. Then, we jump right into the difference between the traditional way of logging (using the Log facade and methods like info) and this new helper.

A quick heads up: since PHP has a built-in log() function (for logarithms), we check out how Laravel deals with naming conflicts by importing its custom log helper from the Illuminate\Support\helpers.php file.

Once everything's set up, we'll use the new log() function to write debug messages and even attach some extra context, like dumping out a user as an array. You'll see the logs show up instantly in both the console and the Laravel log files. While this helper defaults to debug-level logs, you can still pass context arrays if needed.

All in all, it's a small but super convenient addition for cleaner, more straightforward logging in your Laravel projects—especially if you find yourself logging a lot during development.

Episode discussion

No comments, yet. Be the first!