Laravel 5.6 Logging Config0:00
Okay, here I have a Laravel 5.5 application open. And if we check our config directory, we're not going to see anything related to logging. But if I now switch over to a fresh install of Laravel 5.6, yeah, now you're going to see a brand new configuration file. So let's take a look. You'll see that now in 5.6, logging has been split into various channels and drivers. Let's take a look at this. Our default channel will be either what we have stored in the .env file or stack. And we'll talk about what stack is in just a moment. If I switch over to ENV, you'll see that you do get an item populated here by default.
Understanding Stack Channels0:33
And we'll talk about what stack is in just a moment. If I switch over to .env, you'll see that you do get an item populated here by default. And once again, that is set to stack. Okay, so what is stack? Let's take a look. Here's our log channels. So now when you log data or information, you can specify any number of portals that you want to send it through. So for example, we have a single driver. And you're very familiar with this.
Logging to Single File0:55
So for example, we have a single driver. And you're very familiar with this. This is where we log something to a single file. Let's take a look at that. route/web. And then right down here, we're just going to log some information. Hello world. All right. So let's see. What would happen at this point?
So let's see. What would happen at this point? Well, at the moment, our default channel is stack. So we're going to scroll down here and we're going to find the one related to stack. And you'll see that, well, at the moment, there's only a single item in this array, but it looks like we can create a stack of various channels that we want to sort of pipe this information to. And that's a way to think of it. You're stacking one channel on top of the other. In this case, though, we're just deferring to single here.
You're stacking one channel on top of the other. In this case, though, we're just deferring to single here. So let's give it a shot. I will go to laravel56.test. And that should trigger it. So we can now go into storage, logs, into our single laravel.log file. And sure enough, there it is. Okay. So now we're starting to understand this a little better. In our .env file, we've specified stack as our default.
So now we're starting to understand this a little better. In our .env file, we've specified stack as our default. But in this case, we could just as easily have chosen single. And once again, your choices will be right here. These are the drivers that you can use. The idea behind stack, though, is if you want to use multiple ones. So for example, maybe you want all basic logging to be sent to your single log file like you're used to. But what about emergencies? Maybe emergencies should be sent to your company Slack channel so that you see it instantly.
Stacking Multiple Channels2:48
And it's April 9th at the time of this recording. All right. Let's head back to our logging.php configuration file. Now you'll see that for each channel, it's fairly basic and easy to understand. For stack, you will list any number of channels below here or any of these channels. So for example, if I were to say single and Slack, that means when I'm logging, well, I want to log to the single channel according to the level specified there. And then I also want to log to the Slack channel once again, according to the level. So what exactly does level refer to? This is going to be the minimum level where the channel kicks into effect, so to speak.
Setting Log Levels6:50
You might have a warning and you might have a critical or error, you know, it's whatever your team happens to use. So this means if we were to switch back and on Slack, we wanted to say, all right, well, you know what? Anything error or higher, maybe you may not want to do that. You might want to go to critical, but anything error or higher we want in Slack. Now that's done. So let's give this a shot. Before we had critical, so I'll keep it there and we'll say log error. Some error has occurred.
Before we had critical, so I'll keep it there and we'll say log error. Some error has occurred. Of course, in real life, this would be at some point in your project's lifecycle where you feel like you need this to take effect. It can be as mundane as a User has canceled their account and you want to log that just in case for record keeping. Or it could be something went wrong in the system that you need to know about. Maybe an account was created, but there's no associated ID or something like that. Anything where you think, you know what, I should log this for later. Anyways, at the moment, Slack isn't going to get it because we said critical and up
So nothing complicated here. You should be able to understand this fairly quickly and it's a big improvement over what we had before. So once again, just your quick recap, you're going to come to your new logging file. You will decide, do you care or are you fine with a basic single channel? If that happens to be the case, then environment, single, you're good to go. Now if you'd like to do multiple channels, in that case you would reach for stack. Or if you need to, don't forget, you can create as many as you want here. Just make sure you set the specific driver to stack. So if I need something unique here, then I can do so.
Defining Stacks On-the-Fly8:43
Just make sure you set the specific driver to stack. So if I need something unique here, then I can do so. Or what you can even do is define your channel on the fly. So for example, I could say log stack and I'm going to use daily and Slack in this case. And now I'm going to post a brand new error. Okay, so let's give this a shot. We'll give it a refresh and if I now come back to Sublime, you'll see that we have one in the daily log file this time. And if I go to Safari, it should be in Slack as well, and it is. All right, you got this.
