Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
PSR-3 is a recommendation a little different to the PSRs discussed so far. Instead of just being a textual document, this PSR has an actual Composer package you can require, full of defined instances. Logging becomes easier and far more robust when working with PSR-3 and a package like Monolog.
-
0:00
In production we don't want to have any of these errors or
-
0:03
exceptions being displayed to the user, but we do need to report these errors and
-
0:06
other information somewhere.
-
0:08
What we want here is logging.
-
0:10
The idea of logging is very similar to how most errors work.
-
0:13
Most general logging systems have common efforts like debug,
-
0:16
info, notice, warning, error, and emergency.
-
0:21
PSR-3 was developed to standardize the interfaces for basic logging interactions.
-
0:26
By itself PSR-3 is just a set of rules and interfaces.
-
0:29
So we need to use an actual logging system that implements PSR-3.
-
0:33
One great logging system is called Monolog.
-
0:37
So, first things first, let's install Monolog Fire composer.
-
0:40
We can use our console to type composer require monolog/monolog.
-
0:50
I'm gonna ask for a version.
-
0:51
You can use tilde 1:10 to say we accept 1:10 or above.
-
0:58
There. Now it's installed.
-
0:59
We won't need our console any more in this lesson.
-
1:02
Now we can open up index.php and take a look at how we use Monolog.
-
1:06
This is an example of using Monolog in a fairly procedural code base.
-
1:10
Again, this is not really represents different higher applications should look.
-
1:13
Really they should be split up into logical files.
-
1:16
But it does show us how we can get Monolog to log stuff quite easily.
-
1:19
So on line three here we're including the composer auto-loader which will
-
1:22
help us load our monolog code.
-
1:25
Down here we're referencing the monolog logger in a use statement.
-
1:30
This will make logger available in the global space.
-
1:33
In line six we're bringing another class into the global space and
-
1:36
this time we're taking the BrowserConsoleHandler.
-
1:40
Monolog uses the term handler to mean,
-
1:42
a class which decides how logs should be sent out to various locations.
-
1:46
There are a lot of different handlers for
-
1:47
sending things to lots of different places.
-
1:49
Such as, outputting to different log files, sys log, the browser console,
-
1:53
emails, or even hosted logging providers like Logly or New Relic.
-
1:57
Now that various classes are available we can instantiate our logger class.
-
2:01
We pass a string to the constructor as a first argument and
-
2:04
this is the name of our logger.
-
2:06
This can be anything you like and it's just generally a name to help you
-
2:09
differentiate logs because multiple logs can be sent to the same location.
-
2:12
The logger instance is then stored in the log variable so
-
2:14
we can work with it further.
-
2:16
After we set up our login, we then want to set up our handlers.
-
2:19
So we use the pushHandler method to add in instances of various different handlers.
-
2:24
Here, we're only adding one handler to the log.
-
2:26
But really, you can add multiple for all sorts of different things.
-
2:30
We're only using the BrowserConsoleHandler.
-
2:32
And you basically use that by creating a new instance and
-
2:34
sending it to the handler.
-
2:36
So, enough of me reading code.
-
2:38
Let's have a little go and see how Monolog works.
-
2:41
So, first of all, log, everything is reference from the log variable from here.
-
2:46
You can run messages like error or warning.
-
2:49
We're gonna start off with some debug.
-
2:52
Now this method is provided by the PSR-3.
-
2:55
So if you use debug, and error, and warning, and
-
2:57
things like that, then they are definitely always gonna stay the same.
-
3:01
Know they work as they take the string and you can say something is happening.
-
3:06
It doesn't have to be a big scary thing it's just letting you know that
-
3:09
something is happening.
-
3:11
So now if we open up our preview we can see how this all works.
-
3:14
If we open up our browser console in Chrome or pretty much whatever else you're
-
3:19
using then you'll see here in the console tab my app, debug, something is happening.
-
3:24
This means our messages are coming through.
-
3:25
Let's get a little bit more adventurous.
-
3:28
Maybe we're trying to debug something that's happening in a loop.
-
3:31
And just to make a really simple little loop here.
-
3:35
Let's just make this loop through ten times, and we'll call it foo.
-
3:37
We can put that down on a new line, four spaces.
-
3:43
Okay, let's save that.
-
3:46
So here we can see the something is
-
3:47
happening message appearing ten different times.
-
3:49
Now this may or may not be useful.
-
3:51
What would be even more useful is if we could pass a context to these messages.
-
3:55
So we can make an array.
-
3:57
This is the short array syntax if you're not familiar.
-
3:59
And we can say it's an associative array, so
-
4:02
pass it a name and then pass it a value.
-
4:05
We turn back to our preview.
-
4:07
We can see that we've got these little carrots and these provide context.
-
4:11
You can put as many different variables in there as you want.
-
4:15
So as well as debug we have warning and we have error.
-
4:24
[BLANK_AUDIO]
-
4:28
We even have critical.
-
4:34
You probably want critical to send an email to the CEO or something.
-
4:38
Like these critical errors, or any use that's super bad.
-
4:42
So, let's make that message appropriate.
-
4:46
Now if we refresh,
-
4:48
we can see there's a whole bunch of debug information happening.
-
4:52
There's a warning, there's an error, and
-
4:53
there's a critical error right at the bottom here.
-
4:56
Now, something we might want to do,
-
4:57
depending on the type of handler we're using, and
-
4:59
how many different handlers we have.
-
5:00
As you might limit which items go in which log.
-
5:04
To do this we can send an extra argument to the handlers and
-
5:06
this argument is a constant supplied by the logger class.
-
5:09
I'll show you what that means.
-
5:11
You go to the browser consort handler and you use logger to access a constant.
-
5:18
By passing it the warning constant we're saying we only want something that's
-
5:21
a warning or higher.
-
5:23
To get back to preview take a look.
-
5:25
Now we've only got our warning, our error, and our critical.
-
5:28
We only want errors and above.
-
5:32
Excellent, and of course.
-
5:36
And there we go.
-
5:37
You'll want to play around with the configuration of which handlers and
-
5:40
which reporting levels to use.
-
5:41
But that is the beauty of a logger like this.
-
5:44
No matter what types of handlers you use you only need to change those
-
5:47
handler set up lines and know the part of your application.
-
5:51
There's an extra benefit to Monolog being PSL 3 compliant.
-
5:54
If you want to use another logger system there is a good chance that,
-
5:57
that system is PSL 3 compliant too.
-
6:00
If they are both compliant, once again,
-
6:01
only the setup code in your application will need to change.
-
6:05
Your entire application can rely on the logger by passing the log
-
6:08
instance around in service locators or by passing it into other classes manually.
-
6:13
Knowing it'll be PSR-3, you don't have to worry about changing any of
-
6:16
the methods your logs use because they'll always be the same.
-
6:20
That's it for this course.
-
6:21
I hope you've learned a lot about standards and best practices.
-
6:24
My name is Phil Sturgeon, and you can reach me at philsturgeon.uk or
-
6:28
@philsturgeon on Twitter.
You need to sign up for Treehouse in order to download course files.
Sign up