Frage

I am trying to create a logfile for a large modular program. The name is dynamically produced with a timestamp and I can't think of a good way to pass the open filename other than to make it a global variable:

import time

def CreateLogFile():
    timestr = time.strftime("%Y%m%d-%H%M%S")
    global LogFile
    filenamestring = timestr + 'LogFile.txt'
    LogFile = open('PrintLogs/' + filenamestring, 'w')

CreateLogFile()

This isn't working well and I was wondering if anyone had a better suggestion. Thanks

War es hilfreich?

Lösung

Just make your own logging module:

import mylogger

log_file_handle = mylogger.get_log()

The get method can (should) handle all the logic of creating a new logfile if you haven't made one yet, or returning a handle to the already-created-this-session one.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top