Welcome back.
So now we're going to add a really cool feature which will be the audit tool feature
where we can keep track of all the activities on our system
from users logging in and creating accounts and things like that.
So
let's go ahead and do that. And for this to happen, we need to import
a special library and module which is going to be the date time. So
I'm going to come in here and I'm gonna say from daytime library,
let us import the datetime module.
OK? And I can add a note in here that says import for date and time.
OK?
Now let us create our function and where can we place that? You know what?
Let's place the function just before the registration function. OK.
So I'm going to come all the way down in here.
Add the note and we can call this one the function
to log user activities,
activities at tea
did not spell that correctly. Activti.
OK?
All right.
I'm gonna call this function the fine and then log
underscore event that's going to be the name of the function
and then the parameter will of course be event,
OK?
At a colon
and fine. So this is where the magic is gonna happen.
Let us pass the actual date and time
into a variable called timestamp. OK? I'm gonna say timestamp equals.
And now we're going to make use of a function
from the datetime module. This function is called datetime dot now.
OK? So this is the function that will retrieve the actual date and time.
OK?
But now I'm gonna add a method in here
dot STRF time.
This method right here is what we're going to use
to actually format
the date and time.
So whether we want to display the year, the month, the day or maybe just the month,
day and then the time, maybe we want to display the hour the minute,
the second things like that.
So we're gonna use this method, the STRF time method.
And now in brackets, we need to indicate
how we want the date and time to be formatted. So
inside, I'm going to add my codes.
So let's do it like this, ok? We're gonna display the year, the month, the day
that will be for the date
and then for the time, we'll simply display how the hour minute and then the second.
How about that?
Ok? Sounds good.
So to do this,
I'm going to use the percentage symbol
and now capital Y, this will represent the year
and then
we can add minus
and then percentage
and then M for the month
and then minus again
percentage for what for the day? Which will be D?
Ok?
And now a space
and now for the actual time itself,
I'm gonna go again, percentage capital H for the hour.
Ok?
And now instead of the minus, ok? We can use the colon
because I think it's better to use
columns whenever you're dealing with actual time,
in my opinion.
So
we can use colon and then M for the minute and then colon again
and then percentage or capital S
and there you go. So this is the format,
year, month, day and then the hour minute and the second.
Ok. And I think that should be fine.
Now,
all we got to do right now is we now need to start logging in all of these activities.
Ok? So what we're gonna do is for the registration function
when the user has registered,
we need to log the event. Ok?
Now we need to come over here and say with
open because we want to create an actual text file
that will represent the audit log. So I'm gonna say with open
and then in brackets, let's call the file audit
underscore log
underscore log dot txt. So this will be the name of the log file
and then what we wanna do, we want to append it. OK? So I'm gonna put the A mode
and then as what as log
underscore file and then add a colon
and then
what we wanna do, we wanna write
to the log file. So I'll say log underscore file
dot Right. And now in brackets,
we can use our F string
and then we can simply add a
colon
and then I'll add my bracket colly braces
timestamp
and then the event and this should be fine.
There you go. So
we're gonna create
a file called auditlog dot txt.
And when we're writing into the file, we're gonna write the timestamp
and then also the event.
So maybe the user created an account at January 6th 2024 at 5:30 p.m. and 14 seconds,
something like that, you know.
So that's exactly what we've done
over here. Awesome. Awesome. Awesome. So now
we need to make sure that each activity is in fact logged.
So
down here, the first activity is going to be the registration, right?
So let's check actually, you know what?
We can even log
when the password wasn't strong enough. How about that? Ok. We can also log that. So
right over here where it says the print feedback,
I can just come over here right now, add a new line and say log underscore event.
And now in brackets,
we can simply say uh registration field for user username. OK?
I like this. What do you think? I think this is fine
registration field for the user username. So
the registration wasn't successful because the password wasn't strong enough.
I like that. So we've locked down over there.
So now let us log the event in a scenario where
the registration was actually successful. And where is that?
We have it on line 56 here that says print
the user registration was successful. So
just underneath it, we can now add our log underscore event.
And now in brackets,
we can simply say f and then
let's have a coats and then in our collar braces,
we can say username.
OK.
And then a user name
uh successfully registered.
How about that?
And there you go. OK.
Now what else do we need to log,
we need to log when
users have also are logged in.
And we have that over here under the login function where it says login successful.
So in here, I'm gonna create a new line
and now we can log the event or log
on those call events. And then in brackets,
we can say F string and then
add our codes, call braces.
So username.
OK.
Uh Let me create some space in here. Username,
successfully logged in
and there you go.
Is there anything else that we need to log?
Ok. There it says the invalid username or password.
We can also log this event in here as well. So we'll log in everything, everything.
OK? So
log event F and then we can add our codes
and then we can say failed login attempts for
the user and then with the username
and
there you go. Ok.
I am liking this thus far.
Is there anything else we need to log,
we can also log in the
third option
where the user actually exits the system.
It's not necessary, but you know, we might as well. So
where it says print exit in the system,
we can just also
uh log
this
actually, let me just do this before the print before the print statement. So
option number three, let's just log
underscore event
and then we can just
say something like a system
exit,
ok? And
there you go. Ok? I think this should work, this should work.
And I'm going to go ahead right now
and run the program, ok?
And let us see. I'm going to register a new account. So I'm gonna go
with number one press enter.
Let's add a user name.
So I'm gonna go with Tracy,
ok? And the password,
Tracy 21.
Hashtag
and there you go. Ok? Awesome. So
it was successful.
Let's first of all make sure that under the users dot Txt, we have Tracy,
that's the hash password
and now the other
log dot TXT.
There you go. Awesome. Awesome. Awesome Tracy successfully registered
and of course, you can see the format right here
2024
the 10th day of November
at 90108.
So we successfully added the log feature as well.
Thank you so much for watching the video. I will see you in the next class.