Introduction: Terminal Logger

This instructable describes the ease of making a python based Logger on Linux based systems. I am doing this on a Raspberry Pi, but it will work on all Linux distros. You will need Superuser Access. In simpler terms, you need to be an admin.

This logger app will be able to save simple lines of text to a .txt file and spit them out when desired.

What You Can Do: By typing "log" in terminal, you will be prompted with a field to enter your log. And on typing "viewlog" in terminal, you will see all the logs you have entered along with their time of entry

With no more delay, lets get started.

Step 1: Login to Superuser

First, you will have to login as superuser. Go to terminal and type:

sudo -s

This will allow you to make admin changes without typing sudo every time. You can log out of this Super Saiyan form by typing:

exit 

Step 2: Make a Directory Name "/log"

Now you have to make a directory named "/log". Note that this directory has been made in root just for convenience. You can make it anywhere, but if you do, remember to change the file paths through the code as well. You can make the directory by typing in:

mkdir /log/

Step 3: Make the Python Code

Now you have to make the code for the Logger. Create the source file by typing in:

sudo nano /log/Logger.py

You will open up a nano editor window. Type in the following:

import datetime

file = "/log/Log.txt"
iput = str(raw_input("\fEnter Log Here : "))
inFile = open(file,'r')
time = datetime.datetime.now().isoformat()
buffer = inFile.read()+"\n"+time+"\n\t"+iput+"\n___________________________________\n"
inFile.close()
outFile = open(file, 'wt')
type(outFile)
outFile.write(buffer)
outFile.close()
print("\nLog updated sucessfully!!\n")

Then control+x to exit, then y to save and then press enter.

Step 4: Make the Log.txt

Now you have to create the log file. You can do so typing in:

nano /log/Log.txt

Then go ahead and type in something like "LOG" for the heading.

Step 5: Make the Alias

Now you have to log out of Superuser by typing:

exit

Then type in:

alias log="sudo python /log/Logger.py"

and

alias viewlog="nano /log/Log.txt"

Step 6: Test Log Command

Type in:

log

in the terminal and you should be prompted to enter a log.

Step 7: Test the Viewlog Command

Type in:

viewlog

in the terminal and you should be prompted a nano open with the "/log/Log.txt" file open. You should also see your log.