Nov 10, 2011

Log your Python script - Python Logging Hello World

This is a basic logging script used in python.




#!/usr/bin/python

import sys
import logging

if (len(sys.argv)==2):
 loglevel=sys.argv[1]
else:
 loglevel="WARNING"

logging.basicConfig(filename='./mylogging.log',\
 format='%(levelname)s : %(asctime)s - %(message)s',\
 level=loglevel)
logging.debug('Logging started.')
logging.warning('Logging started.')

print "Hello World."
logging.debug("Print :- Hello World.")

logging.debug("End.")
logging.warning("End.")
exit(0)


Here I'm using a library called "logging" to do the logging for me. You can statically give the log level without passing it as an argument. And also there are many log levels you can use according to your need.

For more details :-  http://docs.python.org/library/logging.html

Your suggestions are welcome.

No comments:

Post a Comment

Your comments are always welcome ...