Terminal Logger
Intro: 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.
2 Comments
seamster 9 years ago
I'm not familiar with this. What would it be used for?
Aditya N G 9 years ago
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