1 00:00:00,009 --> 00:00:00,829 Welcome back. 2 00:00:00,839 --> 00:00:05,949 So now we're going to add a really cool feature which will be the audit tool feature 3 00:00:06,190 --> 00:00:10,670 where we can keep track of all the activities on our system 4 00:00:10,680 --> 00:00:14,500 from users logging in and creating accounts and things like that. 5 00:00:14,510 --> 00:00:14,789 So 6 00:00:15,069 --> 00:00:20,040 let's go ahead and do that. And for this to happen, we need to import 7 00:00:20,659 --> 00:00:25,010 a special library and module which is going to be the date time. So 8 00:00:25,409 --> 00:00:32,069 I'm going to come in here and I'm gonna say from daytime library, 9 00:00:32,630 --> 00:00:35,630 let us import the datetime module. 10 00:00:36,200 --> 00:00:41,310 OK? And I can add a note in here that says import for date and time. 11 00:00:41,520 --> 00:00:42,069 OK? 12 00:00:42,650 --> 00:00:48,549 Now let us create our function and where can we place that? You know what? 13 00:00:48,740 --> 00:00:52,790 Let's place the function just before the registration function. OK. 14 00:00:52,799 --> 00:00:54,630 So I'm going to come all the way down in here. 15 00:00:55,669 --> 00:01:00,689 Add the note and we can call this one the function 16 00:01:02,069 --> 00:01:05,370 to log user activities, 17 00:01:06,830 --> 00:01:08,709 activities at tea 18 00:01:09,889 --> 00:01:11,650 did not spell that correctly. Activti. 19 00:01:13,529 --> 00:01:14,150 OK? 20 00:01:14,639 --> 00:01:15,080 All right. 21 00:01:15,089 --> 00:01:17,809 I'm gonna call this function the fine and then log 22 00:01:17,819 --> 00:01:20,669 underscore event that's going to be the name of the function 23 00:01:20,980 --> 00:01:24,230 and then the parameter will of course be event, 24 00:01:24,559 --> 00:01:25,139 OK? 25 00:01:26,720 --> 00:01:27,440 At a colon 26 00:01:28,129 --> 00:01:31,510 and fine. So this is where the magic is gonna happen. 27 00:01:31,699 --> 00:01:35,199 Let us pass the actual date and time 28 00:01:35,400 --> 00:01:41,260 into a variable called timestamp. OK? I'm gonna say timestamp equals. 29 00:01:41,669 --> 00:01:44,839 And now we're going to make use of a function 30 00:01:45,069 --> 00:01:52,349 from the datetime module. This function is called datetime dot now. 31 00:01:52,699 --> 00:01:57,160 OK? So this is the function that will retrieve the actual date and time. 32 00:01:57,269 --> 00:01:57,870 OK? 33 00:01:58,300 --> 00:02:01,739 But now I'm gonna add a method in here 34 00:02:02,839 --> 00:02:07,889 dot STRF time. 35 00:02:08,300 --> 00:02:11,380 This method right here is what we're going to use 36 00:02:11,500 --> 00:02:13,229 to actually format 37 00:02:13,369 --> 00:02:15,050 the date and time. 38 00:02:15,059 --> 00:02:19,990 So whether we want to display the year, the month, the day or maybe just the month, 39 00:02:20,000 --> 00:02:23,809 day and then the time, maybe we want to display the hour the minute, 40 00:02:23,820 --> 00:02:24,770 the second things like that. 41 00:02:24,970 --> 00:02:29,250 So we're gonna use this method, the STRF time method. 42 00:02:29,610 --> 00:02:32,160 And now in brackets, we need to indicate 43 00:02:32,339 --> 00:02:36,160 how we want the date and time to be formatted. So 44 00:02:37,050 --> 00:02:39,000 inside, I'm going to add my codes. 45 00:02:39,380 --> 00:02:44,580 So let's do it like this, ok? We're gonna display the year, the month, the day 46 00:02:44,770 --> 00:02:46,330 that will be for the date 47 00:02:46,509 --> 00:02:51,479 and then for the time, we'll simply display how the hour minute and then the second. 48 00:02:51,509 --> 00:02:52,460 How about that? 49 00:02:52,610 --> 00:02:53,559 Ok? Sounds good. 50 00:02:53,710 --> 00:02:54,979 So to do this, 51 00:02:55,500 --> 00:02:57,850 I'm going to use the percentage symbol 52 00:02:58,050 --> 00:03:01,059 and now capital Y, this will represent the year 53 00:03:01,699 --> 00:03:02,759 and then 54 00:03:02,979 --> 00:03:04,669 we can add minus 55 00:03:05,029 --> 00:03:06,479 and then percentage 56 00:03:06,789 --> 00:03:09,080 and then M for the month 57 00:03:09,479 --> 00:03:10,839 and then minus again 58 00:03:11,009 --> 00:03:14,320 percentage for what for the day? Which will be D? 59 00:03:14,899 --> 00:03:15,570 Ok? 60 00:03:15,800 --> 00:03:17,429 And now a space 61 00:03:17,880 --> 00:03:21,199 and now for the actual time itself, 62 00:03:21,380 --> 00:03:25,539 I'm gonna go again, percentage capital H for the hour. 63 00:03:25,919 --> 00:03:26,520 Ok? 64 00:03:26,860 --> 00:03:31,589 And now instead of the minus, ok? We can use the colon 65 00:03:32,039 --> 00:03:33,250 because I think it's better to use 66 00:03:33,259 --> 00:03:36,089 columns whenever you're dealing with actual time, 67 00:03:36,100 --> 00:03:36,820 in my opinion. 68 00:03:37,029 --> 00:03:37,529 So 69 00:03:38,270 --> 00:03:42,410 we can use colon and then M for the minute and then colon again 70 00:03:42,649 --> 00:03:45,399 and then percentage or capital S 71 00:03:45,860 --> 00:03:48,880 and there you go. So this is the format, 72 00:03:49,179 --> 00:03:52,539 year, month, day and then the hour minute and the second. 73 00:03:52,869 --> 00:03:55,610 Ok. And I think that should be fine. 74 00:03:55,770 --> 00:03:56,039 Now, 75 00:03:56,050 --> 00:04:01,070 all we got to do right now is we now need to start logging in all of these activities. 76 00:04:01,500 --> 00:04:05,169 Ok? So what we're gonna do is for the registration function 77 00:04:05,460 --> 00:04:07,630 when the user has registered, 78 00:04:07,779 --> 00:04:11,520 we need to log the event. Ok? 79 00:04:11,800 --> 00:04:14,839 Now we need to come over here and say with 80 00:04:15,679 --> 00:04:18,899 open because we want to create an actual text file 81 00:04:19,059 --> 00:04:22,029 that will represent the audit log. So I'm gonna say with open 82 00:04:22,459 --> 00:04:25,799 and then in brackets, let's call the file audit 83 00:04:26,329 --> 00:04:28,459 underscore log 84 00:04:30,510 --> 00:04:35,209 underscore log dot txt. So this will be the name of the log file 85 00:04:35,690 --> 00:04:40,350 and then what we wanna do, we want to append it. OK? So I'm gonna put the A mode 86 00:04:40,700 --> 00:04:43,489 and then as what as log 87 00:04:43,959 --> 00:04:46,929 underscore file and then add a colon 88 00:04:47,299 --> 00:04:48,250 and then 89 00:04:48,510 --> 00:04:50,369 what we wanna do, we wanna write 90 00:04:50,529 --> 00:04:53,000 to the log file. So I'll say log underscore file 91 00:04:53,500 --> 00:04:56,179 dot Right. And now in brackets, 92 00:04:56,380 --> 00:04:58,290 we can use our F string 93 00:04:58,880 --> 00:05:00,750 and then we can simply add a 94 00:05:01,049 --> 00:05:02,170 colon 95 00:05:02,529 --> 00:05:05,320 and then I'll add my bracket colly braces 96 00:05:05,690 --> 00:05:06,820 timestamp 97 00:05:07,359 --> 00:05:11,220 and then the event and this should be fine. 98 00:05:11,850 --> 00:05:13,959 There you go. So 99 00:05:14,140 --> 00:05:15,390 we're gonna create 100 00:05:15,750 --> 00:05:18,329 a file called auditlog dot txt. 101 00:05:18,820 --> 00:05:23,029 And when we're writing into the file, we're gonna write the timestamp 102 00:05:23,220 --> 00:05:25,149 and then also the event. 103 00:05:25,459 --> 00:05:33,679 So maybe the user created an account at January 6th 2024 at 5:30 p.m. and 14 seconds, 104 00:05:33,690 --> 00:05:34,329 something like that, you know. 105 00:05:34,339 --> 00:05:35,910 So that's exactly what we've done 106 00:05:36,160 --> 00:05:39,429 over here. Awesome. Awesome. Awesome. So now 107 00:05:39,679 --> 00:05:43,720 we need to make sure that each activity is in fact logged. 108 00:05:43,899 --> 00:05:44,420 So 109 00:05:44,679 --> 00:05:49,040 down here, the first activity is going to be the registration, right? 110 00:05:49,329 --> 00:05:51,989 So let's check actually, you know what? 111 00:05:52,309 --> 00:05:53,859 We can even log 112 00:05:54,100 --> 00:05:59,140 when the password wasn't strong enough. How about that? Ok. We can also log that. So 113 00:05:59,359 --> 00:06:02,339 right over here where it says the print feedback, 114 00:06:02,989 --> 00:06:07,420 I can just come over here right now, add a new line and say log underscore event. 115 00:06:07,950 --> 00:06:09,730 And now in brackets, 116 00:06:10,329 --> 00:06:15,250 we can simply say uh registration field for user username. OK? 117 00:06:15,690 --> 00:06:17,690 I like this. What do you think? I think this is fine 118 00:06:18,470 --> 00:06:21,760 registration field for the user username. So 119 00:06:22,500 --> 00:06:25,829 the registration wasn't successful because the password wasn't strong enough. 120 00:06:25,839 --> 00:06:27,959 I like that. So we've locked down over there. 121 00:06:28,250 --> 00:06:31,470 So now let us log the event in a scenario where 122 00:06:31,730 --> 00:06:35,250 the registration was actually successful. And where is that? 123 00:06:35,380 --> 00:06:38,160 We have it on line 56 here that says print 124 00:06:38,559 --> 00:06:42,100 the user registration was successful. So 125 00:06:42,619 --> 00:06:47,290 just underneath it, we can now add our log underscore event. 126 00:06:47,790 --> 00:06:49,390 And now in brackets, 127 00:06:49,660 --> 00:06:52,359 we can simply say f and then 128 00:06:52,920 --> 00:06:56,760 let's have a coats and then in our collar braces, 129 00:06:57,119 --> 00:06:59,190 we can say username. 130 00:07:00,420 --> 00:07:01,109 OK. 131 00:07:02,160 --> 00:07:03,980 And then a user name 132 00:07:04,890 --> 00:07:08,920 uh successfully registered. 133 00:07:10,589 --> 00:07:11,470 How about that? 134 00:07:11,989 --> 00:07:14,739 And there you go. OK. 135 00:07:14,940 --> 00:07:17,279 Now what else do we need to log, 136 00:07:18,130 --> 00:07:19,709 we need to log when 137 00:07:19,950 --> 00:07:23,149 users have also are logged in. 138 00:07:23,579 --> 00:07:27,790 And we have that over here under the login function where it says login successful. 139 00:07:27,950 --> 00:07:30,630 So in here, I'm gonna create a new line 140 00:07:30,799 --> 00:07:32,760 and now we can log the event or log 141 00:07:33,260 --> 00:07:36,470 on those call events. And then in brackets, 142 00:07:36,920 --> 00:07:39,850 we can say F string and then 143 00:07:40,160 --> 00:07:43,000 add our codes, call braces. 144 00:07:43,529 --> 00:07:44,920 So username. 145 00:07:45,059 --> 00:07:45,709 OK. 146 00:07:46,739 --> 00:07:50,329 Uh Let me create some space in here. Username, 147 00:07:51,470 --> 00:07:54,519 successfully logged in 148 00:07:55,630 --> 00:07:57,690 and there you go. 149 00:07:57,829 --> 00:08:00,570 Is there anything else that we need to log? 150 00:08:01,260 --> 00:08:04,320 Ok. There it says the invalid username or password. 151 00:08:04,329 --> 00:08:09,170 We can also log this event in here as well. So we'll log in everything, everything. 152 00:08:09,179 --> 00:08:09,880 OK? So 153 00:08:10,290 --> 00:08:13,559 log event F and then we can add our codes 154 00:08:13,739 --> 00:08:18,510 and then we can say failed login attempts for 155 00:08:18,779 --> 00:08:21,119 the user and then with the username 156 00:08:21,809 --> 00:08:22,519 and 157 00:08:22,929 --> 00:08:24,760 there you go. Ok. 158 00:08:25,149 --> 00:08:27,850 I am liking this thus far. 159 00:08:28,040 --> 00:08:30,170 Is there anything else we need to log, 160 00:08:30,350 --> 00:08:32,130 we can also log in the 161 00:08:32,320 --> 00:08:33,609 third option 162 00:08:34,460 --> 00:08:37,359 where the user actually exits the system. 163 00:08:37,619 --> 00:08:40,690 It's not necessary, but you know, we might as well. So 164 00:08:40,919 --> 00:08:43,090 where it says print exit in the system, 165 00:08:43,308 --> 00:08:44,799 we can just also 166 00:08:45,840 --> 00:08:47,460 uh log 167 00:08:47,809 --> 00:08:48,650 this 168 00:08:49,979 --> 00:08:53,169 actually, let me just do this before the print before the print statement. So 169 00:08:53,549 --> 00:08:55,830 option number three, let's just log 170 00:08:56,820 --> 00:08:58,409 underscore event 171 00:08:59,700 --> 00:09:00,799 and then we can just 172 00:09:01,159 --> 00:09:03,179 say something like a system 173 00:09:03,469 --> 00:09:04,159 exit, 174 00:09:05,030 --> 00:09:06,349 ok? And 175 00:09:06,770 --> 00:09:11,909 there you go. Ok? I think this should work, this should work. 176 00:09:11,919 --> 00:09:14,049 And I'm going to go ahead right now 177 00:09:14,700 --> 00:09:17,780 and run the program, ok? 178 00:09:18,260 --> 00:09:22,369 And let us see. I'm going to register a new account. So I'm gonna go 179 00:09:22,840 --> 00:09:25,479 with number one press enter. 180 00:09:25,940 --> 00:09:28,849 Let's add a user name. 181 00:09:29,150 --> 00:09:31,010 So I'm gonna go with Tracy, 182 00:09:31,299 --> 00:09:33,049 ok? And the password, 183 00:09:33,679 --> 00:09:36,219 Tracy 21. 184 00:09:37,960 --> 00:09:38,900 Hashtag 185 00:09:40,190 --> 00:09:42,250 and there you go. Ok? Awesome. So 186 00:09:42,929 --> 00:09:43,789 it was successful. 187 00:09:43,799 --> 00:09:46,869 Let's first of all make sure that under the users dot Txt, we have Tracy, 188 00:09:46,880 --> 00:09:48,030 that's the hash password 189 00:09:48,190 --> 00:09:48,809 and now the other 190 00:09:48,940 --> 00:09:49,909 log dot TXT. 191 00:09:50,049 --> 00:09:53,849 There you go. Awesome. Awesome. Awesome Tracy successfully registered 192 00:09:54,020 --> 00:09:55,989 and of course, you can see the format right here 193 00:09:56,520 --> 00:09:57,719 2024 194 00:09:58,080 --> 00:10:01,229 the 10th day of November 195 00:10:01,510 --> 00:10:04,510 at 90108. 196 00:10:04,849 --> 00:10:09,349 So we successfully added the log feature as well. 197 00:10:09,710 --> 00:10:12,750 Thank you so much for watching the video. I will see you in the next class.