You know, there is a logging module!
import logging
import os
logging.basicConfig(filename='tmp.log',
format='%(levelname)s %(asctime)s :: %(message)s',
level=logging.DEBUG)
# format is a formatter string, level shows what level of logs it will record
# in this case it is everything!
# Levels are as follows from most to least critical
# CRITICAL
# ERROR
# WARNING
# INFO
# DEBUG
do = True
yes = True
do_the_work = lambda: None
def main():
logging.debug("Inside of main()")
if do:
logging.debug("Inside of if do:")
do_the_work()
logging.debug("doing work")
if yes:
logging.debug("inside of if yes:")
do_the_work()
logging.debug("doing work")
for list in os.listdir('.'): # there were three files in my folder
logging.debug("inside of for each files/dirs calling list")
print('python')
logging.debug("calling sys.system python")
logging.debug("executing python with give dir path")
Which will produce an output of:
DEBUG 2015-03-18 12:26:59,272 :: Inside of main()
DEBUG 2015-03-18 12:26:59,272 :: Inside of if do:
DEBUG 2015-03-18 12:26:59,272 :: doing work
DEBUG 2015-03-18 12:26:59,272 :: inside of if yes:
DEBUG 2015-03-18 12:26:59,272 :: doing work
DEBUG 2015-03-18 12:26:59,272 :: inside of for each files/dirs calling list
DEBUG 2015-03-18 12:26:59,272 :: calling sys.system python
DEBUG 2015-03-18 12:26:59,272 :: executing python with give dir path
DEBUG 2015-03-18 12:26:59,272 :: inside of for each files/dirs calling list
DEBUG 2015-03-18 12:26:59,272 :: calling sys.system python
DEBUG 2015-03-18 12:26:59,272 :: executing python with give dir path
DEBUG 2015-03-18 12:26:59,272 :: inside of for each files/dirs calling list
DEBUG 2015-03-18 12:26:59,272 :: calling sys.system python
DEBUG 2015-03-18 12:26:59,272 :: executing python with give dir path
You can easily catch exceptions and have them throw more critical events.
try:
really_important_method()
except EndOfTheWorldError:
logging.critical("Duck and cover boys, it's gonna blow.")
nohupin linux.. where it records all the log innohup.out. All I want to do is log each step of the coding in some output file.. it could txt or something else.