1 00:00:00,630 --> 00:00:03,100 In production we don't want to have any of these errors or 2 00:00:03,100 --> 00:00:06,520 exceptions being displayed to the user, but we do need to report these errors and 3 00:00:06,520 --> 00:00:08,360 other information somewhere. 4 00:00:08,360 --> 00:00:10,180 What we want here is logging. 5 00:00:10,180 --> 00:00:13,420 The idea of logging is very similar to how most errors work. 6 00:00:13,420 --> 00:00:16,540 Most general logging systems have common efforts like debug, 7 00:00:16,540 --> 00:00:19,980 info, notice, warning, error, and emergency. 8 00:00:21,515 --> 00:00:26,190 PSR-3 was developed to standardize the interfaces for basic logging interactions. 9 00:00:26,190 --> 00:00:29,720 By itself PSR-3 is just a set of rules and interfaces. 10 00:00:29,720 --> 00:00:33,290 So we need to use an actual logging system that implements PSR-3. 11 00:00:33,290 --> 00:00:35,830 One great logging system is called Monolog. 12 00:00:37,330 --> 00:00:40,455 So, first things first, let's install Monolog Fire composer. 13 00:00:40,455 --> 00:00:50,160 We can use our console to type composer require monolog/monolog. 14 00:00:50,160 --> 00:00:51,270 I'm gonna ask for a version. 15 00:00:51,270 --> 00:00:56,220 You can use tilde 1:10 to say we accept 1:10 or above. 16 00:00:58,170 --> 00:00:59,950 There. Now it's installed. 17 00:00:59,950 --> 00:01:02,250 We won't need our console any more in this lesson. 18 00:01:02,250 --> 00:01:06,030 Now we can open up index.php and take a look at how we use Monolog. 19 00:01:06,030 --> 00:01:10,390 This is an example of using Monolog in a fairly procedural code base. 20 00:01:10,390 --> 00:01:13,770 Again, this is not really represents different higher applications should look. 21 00:01:13,770 --> 00:01:16,030 Really they should be split up into logical files. 22 00:01:16,030 --> 00:01:19,700 But it does show us how we can get Monolog to log stuff quite easily. 23 00:01:19,700 --> 00:01:22,920 So on line three here we're including the composer auto-loader which will 24 00:01:22,920 --> 00:01:25,520 help us load our monolog code. 25 00:01:25,520 --> 00:01:30,130 Down here we're referencing the monolog logger in a use statement. 26 00:01:30,130 --> 00:01:33,258 This will make logger available in the global space. 27 00:01:33,258 --> 00:01:36,740 In line six we're bringing another class into the global space and 28 00:01:36,740 --> 00:01:40,465 this time we're taking the BrowserConsoleHandler. 29 00:01:40,465 --> 00:01:42,590 Monolog uses the term handler to mean, 30 00:01:42,590 --> 00:01:46,240 a class which decides how logs should be sent out to various locations. 31 00:01:46,240 --> 00:01:47,350 There are a lot of different handlers for 32 00:01:47,350 --> 00:01:49,190 sending things to lots of different places. 33 00:01:49,190 --> 00:01:53,510 Such as, outputting to different log files, sys log, the browser console, 34 00:01:53,510 --> 00:01:57,450 emails, or even hosted logging providers like Logly or New Relic. 35 00:01:57,450 --> 00:02:01,440 Now that various classes are available we can instantiate our logger class. 36 00:02:01,440 --> 00:02:04,480 We pass a string to the constructor as a first argument and 37 00:02:04,480 --> 00:02:06,340 this is the name of our logger. 38 00:02:06,340 --> 00:02:09,040 This can be anything you like and it's just generally a name to help you 39 00:02:09,040 --> 00:02:12,090 differentiate logs because multiple logs can be sent to the same location. 40 00:02:12,090 --> 00:02:14,980 The logger instance is then stored in the log variable so 41 00:02:14,980 --> 00:02:16,520 we can work with it further. 42 00:02:16,520 --> 00:02:19,860 After we set up our login, we then want to set up our handlers. 43 00:02:19,860 --> 00:02:24,650 So we use the pushHandler method to add in instances of various different handlers. 44 00:02:24,650 --> 00:02:26,680 Here, we're only adding one handler to the log. 45 00:02:26,680 --> 00:02:30,070 But really, you can add multiple for all sorts of different things. 46 00:02:30,070 --> 00:02:32,070 We're only using the BrowserConsoleHandler. 47 00:02:32,070 --> 00:02:34,950 And you basically use that by creating a new instance and 48 00:02:34,950 --> 00:02:36,490 sending it to the handler. 49 00:02:36,490 --> 00:02:38,470 So, enough of me reading code. 50 00:02:38,470 --> 00:02:41,590 Let's have a little go and see how Monolog works. 51 00:02:41,590 --> 00:02:46,950 So, first of all, log, everything is reference from the log variable from here. 52 00:02:46,950 --> 00:02:49,630 You can run messages like error or warning. 53 00:02:49,630 --> 00:02:52,800 We're gonna start off with some debug. 54 00:02:52,800 --> 00:02:55,210 Now this method is provided by the PSR-3. 55 00:02:55,210 --> 00:02:57,580 So if you use debug, and error, and warning, and 56 00:02:57,580 --> 00:03:01,030 things like that, then they are definitely always gonna stay the same. 57 00:03:01,030 --> 00:03:06,570 Know they work as they take the string and you can say something is happening. 58 00:03:06,570 --> 00:03:09,520 It doesn't have to be a big scary thing it's just letting you know that 59 00:03:09,520 --> 00:03:10,630 something is happening. 60 00:03:11,840 --> 00:03:14,400 So now if we open up our preview we can see how this all works. 61 00:03:14,400 --> 00:03:19,130 If we open up our browser console in Chrome or pretty much whatever else you're 62 00:03:19,130 --> 00:03:24,700 using then you'll see here in the console tab my app, debug, something is happening. 63 00:03:24,700 --> 00:03:25,880 This means our messages are coming through. 64 00:03:25,880 --> 00:03:27,610 Let's get a little bit more adventurous. 65 00:03:28,930 --> 00:03:31,880 Maybe we're trying to debug something that's happening in a loop. 66 00:03:31,880 --> 00:03:35,040 And just to make a really simple little loop here. 67 00:03:35,040 --> 00:03:37,970 Let's just make this loop through ten times, and we'll call it foo. 68 00:03:37,970 --> 00:03:43,490 We can put that down on a new line, four spaces. 69 00:03:43,490 --> 00:03:44,690 Okay, let's save that. 70 00:03:46,430 --> 00:03:47,520 So here we can see the something is 71 00:03:47,520 --> 00:03:49,920 happening message appearing ten different times. 72 00:03:49,920 --> 00:03:51,070 Now this may or may not be useful. 73 00:03:51,070 --> 00:03:55,470 What would be even more useful is if we could pass a context to these messages. 74 00:03:55,470 --> 00:03:57,030 So we can make an array. 75 00:03:57,030 --> 00:03:59,410 This is the short array syntax if you're not familiar. 76 00:03:59,410 --> 00:04:02,200 And we can say it's an associative array, so 77 00:04:02,200 --> 00:04:05,540 pass it a name and then pass it a value. 78 00:04:05,540 --> 00:04:07,040 We turn back to our preview. 79 00:04:07,040 --> 00:04:11,120 We can see that we've got these little carrots and these provide context. 80 00:04:11,120 --> 00:04:13,460 You can put as many different variables in there as you want. 81 00:04:15,230 --> 00:04:24,487 So as well as debug we have warning and we have error. 82 00:04:24,487 --> 00:04:28,923 [BLANK_AUDIO] 83 00:04:28,923 --> 00:04:32,030 We even have critical. 84 00:04:34,850 --> 00:04:38,500 You probably want critical to send an email to the CEO or something. 85 00:04:38,500 --> 00:04:40,770 Like these critical errors, or any use that's super bad. 86 00:04:42,040 --> 00:04:44,510 So, let's make that message appropriate. 87 00:04:46,590 --> 00:04:47,470 Now if we refresh, 88 00:04:48,770 --> 00:04:52,510 we can see there's a whole bunch of debug information happening. 89 00:04:52,510 --> 00:04:53,770 There's a warning, there's an error, and 90 00:04:53,770 --> 00:04:56,100 there's a critical error right at the bottom here. 91 00:04:56,100 --> 00:04:57,410 Now, something we might want to do, 92 00:04:57,410 --> 00:04:59,020 depending on the type of handler we're using, and 93 00:04:59,020 --> 00:05:00,980 how many different handlers we have. 94 00:05:00,980 --> 00:05:04,050 As you might limit which items go in which log. 95 00:05:04,050 --> 00:05:06,690 To do this we can send an extra argument to the handlers and 96 00:05:06,690 --> 00:05:09,870 this argument is a constant supplied by the logger class. 97 00:05:09,870 --> 00:05:11,500 I'll show you what that means. 98 00:05:11,500 --> 00:05:17,400 You go to the browser consort handler and you use logger to access a constant. 99 00:05:18,420 --> 00:05:21,450 By passing it the warning constant we're saying we only want something that's 100 00:05:21,450 --> 00:05:23,340 a warning or higher. 101 00:05:23,340 --> 00:05:25,520 To get back to preview take a look. 102 00:05:25,520 --> 00:05:28,770 Now we've only got our warning, our error, and our critical. 103 00:05:28,770 --> 00:05:30,090 We only want errors and above. 104 00:05:32,160 --> 00:05:34,150 Excellent, and of course. 105 00:05:36,170 --> 00:05:37,630 And there we go. 106 00:05:37,630 --> 00:05:40,170 You'll want to play around with the configuration of which handlers and 107 00:05:40,170 --> 00:05:41,780 which reporting levels to use. 108 00:05:41,780 --> 00:05:44,040 But that is the beauty of a logger like this. 109 00:05:44,040 --> 00:05:47,090 No matter what types of handlers you use you only need to change those 110 00:05:47,090 --> 00:05:49,310 handler set up lines and know the part of your application. 111 00:05:51,130 --> 00:05:54,660 There's an extra benefit to Monolog being PSL 3 compliant. 112 00:05:54,660 --> 00:05:57,390 If you want to use another logger system there is a good chance that, 113 00:05:57,390 --> 00:06:00,150 that system is PSL 3 compliant too. 114 00:06:00,150 --> 00:06:01,890 If they are both compliant, once again, 115 00:06:01,890 --> 00:06:04,389 only the setup code in your application will need to change. 116 00:06:05,750 --> 00:06:08,710 Your entire application can rely on the logger by passing the log 117 00:06:08,710 --> 00:06:12,630 instance around in service locators or by passing it into other classes manually. 118 00:06:13,640 --> 00:06:16,940 Knowing it'll be PSR-3, you don't have to worry about changing any of 119 00:06:16,940 --> 00:06:19,270 the methods your logs use because they'll always be the same. 120 00:06:20,630 --> 00:06:21,910 That's it for this course. 121 00:06:21,910 --> 00:06:24,860 I hope you've learned a lot about standards and best practices. 122 00:06:24,860 --> 00:06:28,368 My name is Phil Sturgeon, and you can reach me at philsturgeon.uk or 123 00:06:28,368 --> 00:06:30,541 @philsturgeon on Twitter.