I've a python 2.5
app that creates a separate thread to work. I want to log to a file, and I'm able to do it from the main thread, but when I log from the other threads it doesnt works.
This happen on the main thread:
log_filename = os.path.join(os.path.dirname(__file__), "log", args[1]+'.log')
logging.basicConfig(filename=log_filename, level=logging.DEBUG)
logging.debug("Hello world!") # this works, line got written to a file
This is how the thread are getting initialized:
worker_thread = threading.Thread(target = MY_worker.MY_worker, args = tuple([task_queue]))
worker_thread.start()
Now from a method that runs on the thread I'm doing:
logging.debug("testing") # this doesnt got printed in the log file
I even tried setting up the log again (inside the thread, just before writting to log):
log_filename = os.path.join(os.path.dirname(__file__), "log", 'sandbox.log')
logging.basicConfig(filename=log_filename, level=logging.DEBUG)
logging.debug("testing") # doesn't works neither.
I tried writting directly to a file, and it worked:
f = open(log_filename,'a')
f.write('some testing message \n')
f.close()
Why is this happening and how to make it work?
Although logging module is thread-safe, it's not process-safe. If you want multiple processes to write to the same log file, then you have to manually take care of the access to your file.
A word about async, Python and filesIf you log into files on disk you are not being fully async and will be using Threads. aiologger was created when we realized that there were no async logging libs to use. At the time, Python's built-in logging infra-structure was fully sync (still is, 3.8 beta is out).
Propagate: Decides whether a log should be propagated to the logger's parent. By default, its value is True. A level: Like the log handler level, the logger level is used to filter out “less important” logs.
All methods on Logger are multi-thread safe.
Are you quite sure it's not some problem unrelated to the logging? The following trivial script behaves as expected, under both Python 2.x and 3.x.
import logging
import threading
import time
def worker(arg):
while not arg['stop']:
logging.debug('Hi from myfunc')
time.sleep(0.5)
def main():
logging.basicConfig(level=logging.DEBUG, format='%(relativeCreated)6d %(threadName)s %(message)s')
info = {'stop': False}
thread = threading.Thread(target=worker, args=(info,))
thread.start()
while True:
try:
logging.debug('Hello from main')
time.sleep(0.75)
except KeyboardInterrupt:
info['stop'] = True
break
thread.join()
if __name__ == '__main__':
main()
When run, it produces
0 Thread-1 Hi from myfunc
1 MainThread Hello from main
502 Thread-1 Hi from myfunc
753 MainThread Hello from main
1003 Thread-1 Hi from myfunc
1504 Thread-1 Hi from myfunc
1505 MainThread Hello from main
2006 Thread-1 Hi from myfunc
2255 MainThread Hello from main
2507 Thread-1 Hi from myfunc
3007 MainThread Hello from main
3009 Thread-1 Hi from myfunc
3510 Thread-1 Hi from myfunc
3759 MainThread Hello from main
4012 Thread-1 Hi from myfunc
until I stop it with Ctrl-C.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With