There are also different log files available within our system, including Apache log. In our Linux distribution, the log files are in the /var/log/ folder within the root filesystem as shown here:
In the preceding screenshot, we can easily see the different types of log files (for instance, authentication log file auth.log, system log file syslog, and kernel log kern.log) available for different operations entries. As we perform operations on Apache log files, as shown previously, we can also perform the same kind of operations on local log files. Let's see an example for parsing one of the log files from before. Create a simple_log.py script and write the following content in it:
f=open('/var/log/kern.log','r')
lines = f.readlines()
for line in lines:
kern_log = line.split()
print(kern_log)
f.close()
Run the script and you will get the output as follows:
student@ubuntu:~$ python3 simple_log.py
Output:
['Dec', '26', '14:39:38', 'ubuntu', 'NetworkManager[795]:', '<info>', '[1545815378.2891]', 'device', '(ens33):', 'state', 'change:', 'prepare', '->', 'config', '(reason', "'none')", '[40', '50', '0]']
['Dec', '26', '14:39:38', 'ubuntu', 'NetworkManager[795]:', '<info>', '[1545815378.2953]', 'device', '(ens33):', 'state', 'change:', 'config', '->', 'ip-config', '(reason', "'none')", '[50', '70', '0]']
['Dec', '26', '14:39:38', 'ubuntu', 'NetworkManager[795]:', '<info>', '[1545815378.2997]', 'dhcp4', '(ens33):', 'activation:', 'beginning', 'transaction', '(timeout', 'in', '45', 'seconds)']
['Dec', '26', '14:39:38', 'ubuntu', 'NetworkManager[795]:', '<info>', '[1545815378.3369]', 'dhcp4', '(ens33):', 'dhclient', 'started', 'with', 'pid', '5221']
['Dec', '26', '14:39:39', 'ubuntu', 'NetworkManager[795]:', '<info>', '[1545815379.0008]', 'address', '192.168.0.108']
['Dec', '26', '14:39:39', 'ubuntu', 'NetworkManager[795]:', '<info>', '[1545815379.0020]', 'plen', '24', '(255.255.255.0)']
['Dec', '26', '14:39:39', 'ubuntu', 'NetworkManager[795]:', '<info>', '[1545815379.0028]', 'gateway', '192.168.0.1']
In the preceding example, first we created one simple file object, f, and opened the kern.log file in it with read mode. After that, we applied the readlines() function over file object to read the data in the file line-by-line in the for loop. Then we applied the split() function on each line of the kernel log file and then printed the whole file using the print function, as can be seen in the output.
Like reading the kernel log file, we can also perform various operations on it, just like we are going to perform some operations now. Now, we are going to access content in the kernel log file through indexing. It is possible because of the split function, as it splits all the information in the file as a different iteration. So, let's see an example of such a condition. Create a simple_log1.py script and put the following script in it:
f=open('/var/log/kern.log','r')
lines = f.readlines()
for line in lines:
kern_log = line.split()[1:3]
print(kern_log)
Run the script and you will get the following output:
student@ubuntu:~$ python3 simple_log1.py
Output:
['26', '14:37:20']
['26', '14:37:20']
['26', '14:37:32']
['26', '14:39:38']
['26', '14:39:38']
['26', '14:39:38']
['26', '14:39:38']
['26', '14:39:38']
['26', '14:39:38']
['26', '14:39:38']
['26', '14:39:38']
['26', '14:39:38']
In the preceding example, we just added [1:3] next to the split function, in other words, slicing. A sub-sequence of a sequence is known as a slice and the operation that extracts a sub-sequence is known as slicing. In our example, we use square brackets ([ ]) as the slice operator and have two integer values inside it, separated by a colon (:). The operator [1:3] returns the part of the sequence from the first element to the third element, including the first but excluding the last. When we slice any sequence, the sub-sequence we got always has the same type as the original sequence from which it was derived.
However, the elements of a list (or tuple) can be of any type; no matter how we apply slicing over it, the derived slice of a list is a list. So, after applying slicing on log file, as a result of that we got the output shown previously.