Time to talk log analysis!
Logs are like the diary of your computer they keep track of everything that happens in a neat little file for you to read later on. But let’s be real here, nobody wants to sift through thousands of lines of text just to find out what happened last Tuesday at 2:30 PM. That’s where Python comes in!
Python has some pretty sweet libraries that can help us analyze our logs and make sense of all the chaos. Let’s take a look at one of them `loguru`. This library is like your personal log assistant, making it easy to read and understand what’s going on with just a few lines of code.
First, let’s install `loguru` using pip:
# Install loguru library using pip
pip install loguru
Now that we have our library installed, let’s create some logs to analyze! We can do this by creating a simple Python script and running it. Here’s an example:
# Import necessary libraries
import time # Importing the time library to use the sleep function
import random # Importing the random library to generate random numbers
from datetime import datetime # Importing the datetime library to get the current date and time
# Define the main function
def main():
for i in range(10): # Looping 10 times
# Do something here...
print("Doing something...") # Printing a message to indicate that something is being done
time.sleep(random.randint(2, 5)) # Pausing the program for a random amount of time between 2 and 5 seconds
# Log some stuff!
with open('my_log.txt', 'a') as f: # Opening a file called 'my_log.txt' in append mode
f.write(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} Doing something...\n") # Writing the current date and time along with the message to the file
print("Done!") # Printing a message to indicate that the program is done
if __name__ == "__main__":
main() # Calling the main function if the script is run directly
This script does some random stuff for 10 iterations and logs each iteration to a file called `my_log.txt`. Now let’s run this script using Python:
# This script does some random stuff for 10 iterations and logs each iteration to a file called `my_log.txt`.
# The purpose of this script is to run a Python script called `my_script.py`.
# The script will run for 10 iterations and log each iteration to a file called `my_log.txt`.
# The following line calls the Python interpreter to run the `my_script.py` file.
python my_script.py
# The `for` loop will run 10 times, with `i` starting at 1 and incrementing by 1 each iteration.
# The `do` keyword indicates the start of the loop's code block.
# The `done` keyword indicates the end of the loop.
for i in {1..10}; do
# The `echo` command prints the value of `i` and a message to the terminal.
# The `>>` operator appends the output to the end of the `my_log.txt` file.
echo "Iteration $i: Running my_script.py" >> my_log.txt
# The `python` command runs the `my_script.py` file.
# The `>>` operator appends the output to the end of the `my_log.txt` file.
python my_script.py >> my_log.txt
# The `echo` command prints a message to the terminal.
# The `>>` operator appends the output to the end of the `my_log.txt` file.
echo "Iteration $i: Finished running my_script.py" >> my_log.txt
done
# The `echo` command prints a final message to the terminal.
# The `>>` operator appends the output to the end of the `my_log.txt` file.
echo "All iterations completed." >> my_log.txt
After running the script, we can open up our log file (`my_log.txt`) and see what happened during each iteration. But who wants to read through all that text? That’s where `loguru` comes in!
First, let’s import `loguru`:
# Import necessary modules
import time
import random
from datetime import datetime
import loguru
# Define main function
def main():
for i in range(10):
# Do something here...
print("Doing something...")
time.sleep(random.randint(2, 5)) # Add a random delay between 2 and 5 seconds
# Log some stuff!
with open('my_log.txt', 'a') as f:
f.write(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} Doing something...\n") # Write current date and time along with the action to the log file
print("Done!")
if __name__ == "__main__":
loguru.setup() # Set up our logger with `loguru` to enable logging
main() # Call the main function to start the program
Now let’s run the script again:
# This script is used to run a python script called "my_script.py"
# The following line executes the python script
python my_script.py
This time, instead of writing to a file, we’re using `loguru` to log everything! Let’s see what happens when we run this script in our terminal:
#!/bin/bash
# This is a bash script that uses loguru to log everything
# The script is executed by running "python my_script.py" in the terminal
# The following line imports the loguru library
import loguru
# The following line creates a log file named "my_log.txt"
loguru.logger.add("my_log.txt")
# The following line creates a function named "do_something" that logs the current time and the action being performed
function do_something() {
loguru.logger.info("Doing something...")
}
# The following line calls the "do_something" function three times, with a delay of 5 seconds between each call
do_something
sleep 5
do_something
sleep 5
do_something
That’s it! We can now easily see what happened during each iteration without having to open up a log file. And if we want to analyze our logs in more detail, `loguru` has some pretty sweet features for that too!