Playing
01. Simple Custom Laravel Logging

Episodes

0%
Your progress
  • Total: 7m
  • Played: 0m
  • Remaining: 7m
Join or sign in to track your progress

Transcript

00:00
In this snippet, we're going to create custom log channels that allow you to log custom formatted messages to separate files. Now, if you've worked with Laravel before,
00:10
you'll know that pretty much all your logs get put into laravel.log. And what we want to do is just separate this out so we have the chance to put specific messages
00:21
into separate files. Now, as an example, we've got a file here called abuse, which will log out if we have any kind of API endpoint abuse or anything else.
00:32
This just helps to keep your logs nice and separate so you can kind of get rid of the clutter of exceptions and see exactly what is happening. So this can be useful in some scenarios.
00:42
And you can see here we've got an API endpoint abuse message with some context about this as well, for example, the user who is abusing this endpoint. So let's go ahead and get started.
00:52
I've got a completely fresh installation of Laravel here. And the only thing I've done is got rid of the contents of the initial route. That's where we're going to be logging this for.
01:03
Just before we go on, let's take a quick look at how Laravel actually logs in the first place. So if we head over to EMV, you can see we've got a log channel here that's set to stack.
01:13
Now, stack is like a multi-channel wrapper. So over in our config, if we open up logging, you can see that by default the log channel is stack if we haven't set anything.
01:24
And this will just go ahead and stack through multiple channels depending on how you're logging things. So we've got daily logs, we've got the ability
01:31
to log to Slack if we want to, paper trail. So if you're not using an external service, this can be really useful. OK, so what we want to do is end up
01:40
with a custom channel in here, which we'll create ourselves. And then we will look at using a formatter to clear up any of the clutter that we might already see. So the first thing that I'm going to do
01:52
is actually look at how we log things. So we can do this in a variety of ways, but the easiest way is to use the log facade. And we can go ahead and choose the level of log.
02:02
So for example, most of the time it will be info, but we can have things like warning, et cetera, et cetera. So we're just going to go for info for now. So if we would say API endpoint abuse in here
02:14
and pass through some information, so for example, the user ID who is abusing a particular endpoint, we could go ahead and log that out.
02:21
And that's going to end up in the Laravel log. So if we come all the way down to the bottom, you can see that we've got a couple of logs just inside of here.
02:28
But this is kind of mixed in with everything else. So we want to create our separate channel for this to happen. So to do this, we just add a new key.
02:36
So let's go ahead and say abuse in here. And let's create out an array with the driver. Now, the driver that we're going to choose here is going to be single.
02:46
This allows us to log to a single file. So we can choose the file that we want to log to. And we can also tap through a formatter as well to format this in a way that we want to.
02:56
The next thing is the path. So this is usually going to go in your storage path along with your other logs. So we're going to go into logs.
03:03
And we're going to call this abuse.log. So that's the file that we want to log to. The next thing is the level. So this depends, but usually we'll just set this to debug.
03:14
So we're just going to go ahead and choose debug for this. So let's just fix that up pretty quickly. You've probably noticed that. And we've now created our own channel.
03:23
Now, to select a channel while we're logging, we basically just use the channel method, and then we give the name of the channel. So in this case, the name of the channel is abuse,
03:32
if I can spell it. And now that should go ahead and log to that separate file for us. Let's try this out.
03:37
So I'm going to give that a refresh, come over, and you can see, sure enough, that has gone and logged that information to here. Now, you might want to customize how this looks,
03:47
depending on the kind of stuff that you're logging. And that's where a custom logger comes in, or a formatter via monologue. And we use the tap key to provide a list of formatters.
04:00
So what this will do is it will take the error that you're passing through to this particular channel, and it will make this available in any of the classes that you define out here.
04:10
So let's go ahead and create our formatter for this. So inside of app, I'm going to create a logging directory. And in here, I'm just going to create out a simple formatter. And let's go ahead and get this working.
04:21
So let's give this a namespace first of all, which is just under app and logging. And let's create a class out here called simple formatter. And this just takes an invoke method.
04:32
So let's go ahead and create out a magic invoke method for this. Now into this invoke method, we get passed in the logger, which we can use to start to format things.
04:44
So for this, what we need to do is grab each of the handlers, the potential handlers for this, and then do anything on them, handlers that we like.
04:52
So let's go ahead and iterate over the handlers. So let's say for each logger, get handlers as handler. And this is in the Laravel documentation. So if you're just following along with this snippet
05:04
and you need this code, you can just go ahead and grab this directly from the Laravel docs. Now for each of the handlers,
05:10
we're going to go ahead and set the formatter for this. So what this allows us to do is create out any kind of format that monologue supports. We're going to go ahead and choose a line formatter.
05:19
It's just the most common. So we're going to instantiate a line formatter. We're going to go ahead and import that from monologue. And then we're just going to pass into here
05:27
the string that we want to see with the information. So for this, let's go ahead and output the date time. So that's pretty important to know when this actually happened.
05:38
You can output things like the channel as well, but this might not be useful. So we can get rid of that. And really importantly,
05:44
we actually want the message that we're actually logging out. So let's go ahead and output the message just inside of here.
05:49
And again, really importantly, the context, which is the array that we passed through to this. So we're going to go ahead and put the context in there as well.
05:58
So that should be enough to make this work. If we come over to logging now, what we can now do is import this into here, and that should work as we like.
06:07
So we just need the fully qualified class name to this. So let's go ahead and say simple formatter and class, and let's make sure we import that at the top. Or of course you can do that inline if you want to
06:18
within that array. It's entirely up to you. Okay, let's try this out. So I'm going to go ahead and hit this again.
06:25
And if we come over to our abuse log, that didn't actually work. Let's just try that once more. And yeah, it's not worked.
06:32
So if this doesn't work, if you head over to your main Laravel log, you can see here we've got unable to create configured logger using emergency logger.
06:39
And that is just because here we see an undefined property get handlers. So even if you end up with an error within the code that you're writing
06:48
to get your logger working, you can go ahead and just fix up anything directly from your main log. Let's try this out again and come over to our abuse log.
06:56
And sure enough, there we go. So now we see the date and time, the message and context as well. So you can pretty much customize this however you like.
07:05
You can go over to the monologue documentation, check out the kind of formatters that you can create. And as long as you've done this, you now have a completely separate file set up
07:15
that you can choose on demand where to log to and the message and the context that you want to log as well. So this can be really useful
07:22
for exactly what we've implemented here, just logging out a list of potential abuses of an API endpoint or pretty much anything you want to do. And of course, what you can do
07:32
is create multiple channels if you want to. So you could create another channel in here for something very specific, use a completely different file name for this,
07:39
it's entirely up to you. But now you know how to create a custom logger or a custom channel, which goes ahead and formats the message and the context passed in using monologue.
1 episode 7 mins

Overview

This snippet walks you through setting up a separate log channel in Laravel, with its own file. We'll also tap into a custom log formatter to choose the output structure. Great for separating different kinds of logs.

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

Episode discussion

No comments, yet. Be the first!