Configuring (and viewing!) logs in Laravel
In this video, I explore the various ways to configure logging in your Laravel application, from simple file-based logging to more advanced tools like Laravel Telescope, Sentry, and more. Whether you're debugging locally or tracking errors in production, Laravel's flexible logging system has you covered with multiple drivers and configurations to fit your needs.
Everything I said
Laravel has a bunch of different ways to configure the logging setup in your application, and that's good 'cause, yes, every application is different, but honestly, every part of a single application is different, and every day on the job is different. Some days, somebody, not you, wrote a bug, and we cannot figure out what is going on, and so we gotta crank up the logging and then just turn it back down later.
Laravel is set up for us to do that. Now, the most basic setup, write logs to a file, run tail on that file. We're gonna look at that. It's a little bit uninspired. There are a few more options for log viewers. We have several local log viewers, a few that you can run, uh, from the command line, some local log viewers that you can run in the browser, so we're gonna look at tel- Laravel Telescope for that.
And then in production, I like to put my logs and errors in Sentry. They are a channel sponsor, so do with that what you will, but I like putting all of that stuff in Sentry. You could also use something like Flare, and then there are a few paid local things that can aid your development should you choose to go down that route. Let's look at a vanilla Laravel application. This is my scaffolded Laravel application.
Let's go ahead and open logging, and we see all kinds of stuff in here. Lots of stuff. We're not gonna go over it all, but I'll give you a run-through really quick. Laravel works in log channels. So you can have different channels for different types of logs, whether that's info, debug, error, uh, emergency, whatever. Um, and every channel can declare its own kind of configuration array.
So for Slack, you may only send m- uh, emergencies, and for the file, you may write all information to the file. Most configuration in Laravel is now done using, uh, environment variables, and that's to make it a little bit easier so you don't have to go muck around with these config files. Um, so you can see here that the default log channel is set to an environment variable called log channel, which defaults to the word stack.
Now, if we come down here and look at all of the log channels, we see a channel called stack, and we're gonna skip this for now. We're gonna come back to this because a stack is a, a conglomeration of multiple channels, which is kind of nice but not a good place to start. So we're gonna skip that, and we're gonna come right down here to the single channel.
And you'll see that the driver is set to single, and the path is set to laravel.log. And the level, importantly, is set to debug. So this is gonna be your noisy log. This is gonna be where everything goes. And so let's come, uh... Let's go over to our ENV and come down here to log channel, and we're just gonna set log channel to single for now.
We got a little bit of an assist there from Laravel Idea. Thank you. So we're gonna set it to single for now, and then let's go to the web.php, and instead of returning a welcome view, let's say, uh, log info, "Hey, we made it." And that looks hideous, doesn't it? Let's simplify that, and we are good to go.
Now, using, uh, Laravel Herd, I have this already all wired up. Laravel Herd is a great application for setting up your local PHP/Laravel environment. And so I have this set up at logs.test, and if we hit that, we should see nothing because we're logging it to a file called laravel.log.
Switching back over here, we can open this up and come down to storage, uh, logs, laravel.log. Hey, we made it. That's all it takes. That's all you have to do to get your logs in Laravel. Now, this isn't a super great experience yet because if we were to hit this a few more times and then come back here, it g- kinda...
Sometimes it takes a second to reload. Sometimes it doesn't notice that the file has changed. And so if I want a streaming set of logs as I'm developing, we can totally do that. When it comes to following our logs, we're gonna look at two separate options. The first is tail, which totally works, but is a little bit uninspired, and the second is pail, which I assume is PHP plus tail.
But it... Laravel Pail is a first-party package that you can install into your Laravel application that adds a little bit of context. So before we do that, let's look at tail. You can say tail -f, which is going to follow your log file. If you were to just say tail, it would do storage, logs, Laravel, and that's it. Command is over.
Now, if you were to say tail f storage, logs, Laravel, it's gonna keep it running. So if we hop back over here and we hit refresh, there we go. The logs are rolling in. This is fine. This does not move me. I'm not inspired by this. Let's check the Laravel documentation on logging, and here's all of the how you can build up your own stacks and everything.
And then the good stuff is down here. Tailing log messages using Pail. If we hop down here, um, debugging, yada, yada. Laravel Pail is a package that allows you to dive in from the command line. Pail is designed to work with any log driver, including Sentry or Flare, which is pretty cool. So let's, uh, kill that guy, and off-screen, I have run this, uh, composer require laravel/pail.
It is a free package. You just have to install it. Now, I can run... Let's see what it says. PHP Artisan Pail. I've aliased PHP Artisan to AR because I type it so often. AR Pail. Okay, we're... get information that we are tailing the logs, and if we hit that again, hey. That's a little bit nicer, isn't it? Um, not only does it look pretty, it tells me the level in a much nicer way, and it shows me the route and the logged in user.
So if that is true, let's come over here, and we'll make a new route of error. And what's another... Yeah, we can do... We can do an error, and we'll say, "Oh, no." And coming back here, instead of... Need a little space here. Um, instead of /test.
We'll do error, and there we should see error oh no. So you see that the route has changed and the little, uh, information badge has changed, but we still get our log out here. So Laravel tail is a pretty... It's a pretty nice way to tail your logs. Now, if you don't fancy the command line, that's totally fine.
It's totally a personal preference. I don't really care. But if you don't like the command line, you have several other options for viewing your logs. The first one that you have is a first party package called Telescope. Now, Telescope is much more than just log viewing. So if we come up here, it says, uh, "It is for your local development environment.
Please do not ship Telescope to production. Um, it provides insights into requests, exceptions, logs, database, queued jobs, mail, notification, cache, scheduled tasks, variable dumps, and more." And so if you install Telescope into your Laravel application, you get greeted with a screen like this, and you can log everything.
Everything that you want, it can end up in Telescope. It is extremely powerful. If you don't like that, there is Laravel Herd, which is what I use to set up my local computer to serve Laravel. All of that part is free. They do have a paid tier that includes stuff like mail trap and viewing logs and a, a bunch of other stuff like that.
You don't have to use it. It's quite nice. There is also Ray, which is, um, similarly a local development tool that you can use for debugging and for logging. It is also paid. So if you want a GUI that is free, Laravel Telescope is probably your best way to go. If you want to use the command line, tail is probably the best way to go.
Now, obviously, all the command line stuff can be used in production, so you can tail or pail in production if you have a server that you can SSH into. I would not use Telescope in production. That would be, um, both slow and dangerous. And then Herd and Ray are only gonna work on, uh, files, so you can't use those in production either. So for that, I would reach for something like Sentry.
Now, let's talk about this configuring of the logs so we can see what a stack is versus just a specific channel. Back in our configuration file of logging.php, we have looked at some of this before. We're gonna continue to skip over stack just for now and look at these other drivers. So every driver has a name. This one is called single, and this is the means by which you address it.
So if we were to hop back over here, you could say, uh, I wanna log to a channel called single or Slack or emergency, which maybe is a Twilio SMS or something like that. And then you can say off of that info warning, whatever. So you can address the logs by name. If you don't address the logs by name, it goes to your default, uh, channel, which in our case, it's defaulting to stack, which is a channel named stack, which
we're going to skip for now. Um, okay, so coming back, you have these multiple drivers. Single just appends it to a single file. You can change the path. If you're gonna ship this to production, I would probably recommend doing daily and keeping logs for however long you want. It's set to 14 days. You could change it to seven. You could change it to 30.
It is, um, of course, it is environment variable driven, so maybe your staging server keeps logs for 60 days and production keeps them for 14, or switch that around depending on, uh, your, your traffic levels and how much disk space you have and how long you need to retain those logs. So moving on, we've got Slack.
The driver is Slack, and this is where you set up your webhook URL, and you can, um, set up emojis because everybody loves emojis. And you can set a level. If you're gonna pipe your logs into Slack, do not pipe all of them into Slack. You will not like that very much. But maybe the most important ones, maybe they should go straight to Slack to get somebody's attention.
Papertrail, standard error, Syslog, error log, error log null. This is a, this is a black hole, so if you send stuff there, don't expect to see it. And then emergency, you can put it on, um, a different path. You can, you can change the driver, but you can put it on a different path as well. And then up here, we can just drop Sentry.
So we can say that the driver is Sentry. You do need the Sentry SDK installed for that, and then you can set the level and whether or not the, um, events that are handled should bubble up in the stack. Let's talk about stacks finally, for goodness sakes. I'm gonna delete that 'cause it's a little bit confusing to explain. So when you have a stack driver, this is a way to aggregate other drivers.
So potentially you want all debug and info messages sent to a file, but you want critical messages sent to Slack and to Sentry, or maybe Slack just gets critical and Sentry gets error and critical. You can do that right here. So you can say, I wanna go to Sentry, I wanna go to daily, and I wanna go to Slack.
And then each one, it'll, it'll forward those messages on to each specific channel, and then each channel gets to decide for themselves, do I handle it? So if we forward an info on to Sentry and we've configured it to be error only, Sentry's gonna ignore it, but daily is gonna handle it because we've configured that to be debug.
So this is a really powerful way to make sure that your logs are getting to the right spot, but you're not overwhelming the consumers, because if you overwhelm Slack, people will ignore it or turn it off, and if you don't put enough information in the daily log, you might not be able to figure out what was going on with a particular bug. So this is a way that you can configure your logging to meet your exact needs.
Hopefully, this is helpful. I know that there are a lot of different ways to configure it, and if you're coming into it brand new, that can be really overwhelming. Um, I totally get that, and I know that the team, uh, of which I am not a part, I know that the Laravel team is always trying to improve their documentation. And so if you find something confusing, reach out or open a PR or an issue on the docs, the docs have their own repo, um, and let them know what you found confusing.
That's always extremely helpful. Unfortunately, all of the given options are very good. And so it's just kind of a matter of deciding, um, do I just wanna straight up tail it? Do I wanna pail? Do I want telescope locally? Do I want Sentry for production? Do I just wanna pipe everything to Slack? I don't know.
That's, that's all good options. I can see a scenario where each one of those things works. And so dig in, read the docs, find out what meets your needs, and if you get confused, please reach out for help. Until the next time, go watch some more of my videos. See ya.