Multiple functions to write into a single log file using Python
<p>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.</p>

<p>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?</p>

<pre><code>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()
</code></pre>