A very simple personal logging application. I use it to manage my TODO/done list and as a quick place to drop a note into so ideas don't get lost.
Basically everything passed into l.py as a parameter is logged to a file as a new line. It also has options to list the last 10 lines and to list all lines which contain certain text.
run python l.py --help for usage info.
Enjoy,
code
"""A very simple personal logging application.
Requires Python 2.3
Brian Dorsey
brian@dorseys.org
"""
__version__ = '0.1.0'
__history__ = [
'2003/10/13 - Created.'
,'2003/10/13 - Simple logging implemented.'
,'2003/10/21 - Add --edit option'
]
# TODO:
import sys
import os
import optparse
import logging
logfile = 'log.txt'
def help():
print
def main(args, options):
"""
This program will log all parameters passed to it as text to a log file.
Defaults to $HOME/log.txt
Basic Usage:
l some text [followed optionally by more text] """
verbose = options.verbose
debug = options.debug
logpath = os.path.join(os.environ['HOME'], logfile)
if debug: print 'logpath:', logpath
log = logging.getLogger()
simpleformat = logging.Formatter('%(asctime)s %(message)s',
'%Y-%m-%d %H:%M')
hdlr = logging.StreamHandler()
hdlr.setLevel(logging.INFO)
hdlr.setFormatter(simpleformat)
log.addHandler(hdlr)
hdlr = logging.FileHandler(logpath)
hdlr.setLevel(logging.INFO)
hdlr.setFormatter(simpleformat)
log.addHandler(hdlr)
log.setLevel(logging.INFO)
if args:
log.info(" ".join(args))
if options.tail:
loglines = open(logpath, 'r').readlines()
print ''.join(loglines[-10:])
if options.edit:
try:
editor = os.environ['EDITOR']
except KeyError:
editor = 'gvim'
cmd = '%s "%s"' % (editor, logpath)
os.execvp(editor, [editor, logpath])
if options.find:
loglines = open(logpath, 'r').readlines()
loglines = [item for item in loglines if options.find.lower() in item.lower()]
print ''.join(loglines)
if __name__ == '__main__':
versiontext = '%prog ' + __version__
parser = optparse.OptionParser(usage=main.__doc__, version=versiontext)
parser.add_option("--history",
action="store_true", dest="history", default=False,
help="show this program's development history and exit.")
parser.add_option("-q", "--quiet",
action="store_false", dest="verbose", default=True,
help="Suppress extra output.")
parser.add_option("-d", "--debug",
action="store_true", dest="debug", default=False,
help="Include debug output.")
parser.add_option("-t", "--tail",
action="store_true", dest="tail", default=False,
help="Show the last few log entries.")
parser.add_option("-f", "--find",
action="store", dest="find", default=None,
help="Show the log entries with this text in them.")
parser.add_option("-e", "--edit",
action="store_true", dest="edit", default=False,
help="Edit the log file using EDITOR environment variable.")
opt, args = parser.parse_args(sys.argv[1:])
if opt.history:
print '\n'.join(__history__)
sys.exit(0)
if len(args) == 1 and args[0].lower() in ('/?', '/h', '/help'):
parser.print_help()
help()
sys.exit(0)
main(args, opt)
comments on the code
I always run this as just "l" followed by my new log entry, for the least amount of typing. To make that work on windows, I have .py files associated with python.exe. On linux I created a symbolic link from "l" to "l.py"
This script works on both windows and linux, I would assume that it works on Macs as well. On windows you will need to have %HOME% set to an existing directory or modify the script to your tastes.
And yes, the logging module is way over-kill for this simple script.