You should take a look at python logging module
EDIT: Sample code:
import logging
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG, filename="logfile", filemode="a+",
format="%(asctime)-15s %(levelname)-8s %(message)s")
logging.info("hello")
Produce a file named "logfile" with content:
2012-10-18 06:40:03,582 INFO hello
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…