Hey everybody welcome to the next video.
So in this video we're gonna be going over and logging now just like configuration files.
Logging is just as important to understand even from a red team's perspective not just so you know what
can be logged but what you should be logging in your own applications whether it's logging to the screen
or logging to a file.
Every tool you make whether it's for yourself or for another team member you need to be aware of the
logging capabilities that need to happen especially when you start looking at larger scale tools.
So if you try to rebuild something similar to Metis Boyd or something that's custom for your own team
you need to be able to log information so that there is no misinformation that happens.
So when something bad happens and you silently catch it let's say your your error handling doesn't tell
you that something went wrong.
You need to still be aware that something did happen from a team's perspective.
So how this is going to work is similar to what we did with the config files we're going to import logging.
So logging is a Python module that allows us to quickly log things in in a relatively simple way.
So the way this works is just like we do with our our config we're going to build a logging object.
But before we can do that we have to set up the basic configuration first.
Right.
So do logging the basic config now.
No space there their basic config.
Now this config takes a few arguments.
The only one we're going to be worrying about in this particular video is something called the file
name.
So we can do filing equal to this.
Now load the file name as it tells us where it wants to log the file.
So if you give it a full path here it will log to that path.
If you give it just a file name it aloud to the local directory you're running the python script from.
In this case we don't care we just wanted to log in our current directory shall say the main dialog.
Right.
So now that we've generated the basic config we told the log where to where to actually log to the next
thing we're going to want to do is build our logger object.
So we do a logger or anything else you want to name it is equal to logging get logger.
So this is going to do is it's going to create an instance of the logger object with that basic config
and passed a reference to it to the logger variable.
So this way we can reference it later because this is a python class or Python object.
What we can do is we can do a few things.
We can log in multiple different levels.
We can log in debug mode which is normally the highest level mode.
So you have the most information being logged here.
We've got info which is the standard mode.
And then we've got error for when an error happens and then critical for when a critical issue happens
that might not be considered an error is something you want the user to be aware of.
So in this case we're gonna set the log level to enforce the way we can do this as we do logger does
set level and then we give it a log level a quick way to do this is to use the logging module and then
just tell us logging dot info logging debug whatever the case might be.
So if we log this into info the way we can start logging here is we can do logger dot then the type
that we want.
So if we want info we do dot info and we pass it a string.
This is a log.
So if we go ahead and run this we should generate a file here called log named dot log with just the
text.
This is a log.
Perfect.
So if we go up here see main dialog we see it's an info.
This is the user which in my case was route that was running it.
And that says this is a log we're going to go and delete this just for now so we can do the same thing
for debug as well since we're in in full mode.
But what I want you to notice is something here.
So we go ahead and we radical we may not log but you'll notice there's nothing in the log.
The reason for that is because you can think of these as numbers.
So info is Level 0.
You can only log what's berms out of imposed level.
Let's say 2.
Right.
So if this is 2 and debug is 4 you can only log things lower than the current log level.
So there's a few exceptions with that such as error and critical which will log into the log regardless.
So even if we're at info here we can do logger dot error and say this is an error and then we'll say
this is this is debug and then we'll do critical as well.
It's a good run that we can see in here.
We have our error in critical but not debug and of course info work as well.
However if we take this same situation
and we change this to debug instead and then we go ahead and we'll clear this file out so that there's
nothing in it go and rerun this.
Now we should get all of them twice a year we ever air are critical and then we moved to the debug level.
So then we see the debug log error in critical.
So understanding logging is important.
Now I would recommend adding any logging feature to an application that you wrote or we'll be writing
it.
It never hurts it takes only like you like you're seeing here just a few lines.
Essentially you can get away with the import the basic config in grabbing the logger and then the log
level should be set to in full by default so you don't even need this line.
So three lines of code and then where you would put your print statement you just do logger dot then
whatever if it's an error dot error in vote dot info.
And for those of you who have bigger applications you can use dot debug and provide more information
than what is normally available such as what's in current current variables and what's in memory.
So you can do stuff like that where having the amount of information needed for somebody using your
application so they can identify what went wrong and also for people that might be maintaining your
code long after you are either still at the company moved to a different team moved to a different company
whatever the case might be you want to make sure that they're able to dissect what's happening while
it's running without having to dig too far into the code itself.