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