I have a Python file with contains a few functions. When the "main" function runs, it runs through the functions. Each function needs to write some text to the same single log file.
I have written the following codes. The file handler is declared here as global in each function. I am wondering if there is a better way to achieve this?
LOG_FILE_NAME = 'TestLog.log'
f = None
def log1():
global f
print 'log1 called'
f.write('log1 result' + '\n')
def log2():
global f
print 'log2 called'
f.write('log2 result' + '\n')
logFileDir = LOG_FILE_NAME
f = open(logFileDir, 'w')
log1()
log2()
f.close()
loggingpackage? It's very simple to set up and use for a single file: docs.python.org/2/howto/logging.html#logging-to-a-file