Introduction: Terminal Logger

Picture of 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

Picture of 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"

Picture of 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

Picture of 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

Picture of 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

Picture of 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

Picture of Test Log Command

Type in:

log

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

Step 7: Test the Viewlog Command

Picture of 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.

Comments

seamster (author)2014-10-22

I'm not familiar with this. What would it be used for?

Aditya N G (author)seamster2014-10-23

Well, you see: some people are using their Linux boxes only iva terminal for long periods of time. And sometimes it may become necessary to log something. Lets say:

1) A command you may want to remember,

2) You just want to log some data that you can later convert to a blog (not part of project)

3) Quickly jot-down an idea for some improvement for the system, etc

This project seeks to create something like a productivity application. However, you may take this project as a means of learning coding.

In the end, I would like to thank you for noticing my existance