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