Sep. 29, 2012

Log Checker in Python

Show all entries in a logfile

This script will show all entries in the file that is specified in the log file
variable. 

Log Checker Script

In this example, I will use the /var/log/syslog file. 

The for loop will go through each line of the log file and the line_split variablewill split it by lines. 

If you just print the line_split, you will see an output similar to this:

>> ['Sep', '27', '15:22:15', 'Virtualbox', 'NetworkManager[710]:', '', 'DNS:'..']

If you want to print each element just add the line_split[element_to_show]
#!/usr/bin/env python
logfile = open("/var/log/syslog", "r")
for line in logfile:
    line_split = line.split()
    print line_split
    list = line_split[0], line_split[1], line_split[2], line_split[4]
    print list
That's it, now you have a script that you can use for checking log files with. 

Recommended Python Training – DataCamp

For Python training, our top recommendation is DataCamp.

Datacamp provides online interactive courses that combine interactive coding challenges with videos from top instructors in the field.

Datacamp has beginner to advanced Python training that programmers of all levels benefit from.

 



Read more about:
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, PythonForBeginners.com only recommend products or services that we try personally and believe will add value to our readers.