简体   繁体   中英

python logger class for multithreading

I'm using my own logger class in my freelance projects for access a logs via ftp and check errors. It writes a separate file for each process which output to stdout via os.getpid() function. This is useful for python multiprocess library. But I'm doing multithreading more often than multiprocessing and I don't know how to improve my code to write a separate file for each thread which outputes to stdout.

class Logger(object):
    def __init__(self, realstdout, path='logs/'):
        today = datetime.datetime.now().isoformat()
        if path[-1] != '/':
            path = path+'/'
        os.mkdir(path + today)

        self.pid = str(os.getpid())
        self.handler = open(path + today + '/' + self.pid + '.txt', 'w', buffering=0)
        self.stdout = realstdout

    def write(self, buf):
        if buf == '\n' or buf == '(Pdb)':
            return
        buf = buf.replace('\n', '#')
        self.handler.write("[{0}]  [{1}] ".format(datetime.datetime.today(), self.pid) + buf + "\n")
        self.stdout.write("[{0}] ".format(self.pid) + buf + "\n")

    def flush(self):
        pass

    def __del__(self):
        self.handler.close()

How to do that?

If the same strategy is used, logging a separate log file for each worker, then there has to be some sort of identifier for each thread. Since each thread is executed in the same process they will all have the same process id.

I believe you could use the thread ident to identify it:

>>> threading.current_thread()
<_MainThread(MainThread, started 139944996378368)>
>>> threading.current_thread().ident
139944996378368

A threading aware logger might be able to create a unique identifier with:

self.pid = str(threading.current_thread().ident)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM