I am attempting to make a logging module for Python that does not work because it fails on creation of the file object.
debug.py:
import os
import datetime
import globals
global fil
fil = None
def init(fname):
     fil = open(fname, 'w+')
     fil.write("# PyIDE Log for" + str(datetime.datetime.now()))
def log(strn):
    currentTime = datetime.datetime.now()
    fil.write(str(currentTime) + ' ' + str(os.getpid()) + ' ' + strn)
    print str(currentTime) + ' ' + str(os.getpid()) + ' ' + strn
def halt():
    fil.close()
fil will not work as None as I get an AttributeError. I also tried creating a dummy object:
fil = open("dummy.tmp","w+")
but the dummy.tmp file is written to instead, even though init() is called before log() is. Obviously you cannot open a new file over an already opened file. I attempted to close fil before init(), but Python said it could not perform write() on a closed file.
This is the code that is accessing debug.py
if os.path.exists(temp):
         os.rename(temp, os.path.join("logs","archived","log-" + str(os.path.getctime(temp)) + ".txt"))
         debug.init(globals.logPath)
         debug.log("Logger initialized!")
I would like to have logging in my program and I cannot find a workaround for this.
 
     
     
     
     
    