Arnold Roa
Arnold Roa

Reputation: 7718

Logging not working in a separate thread

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?

Upvotes: 8

Views: 8925

Answers (2)

Hamdam Muqimov
Hamdam Muqimov

Reputation: 389

I dunno why, but in my case, when i try to add thread, my function didn't run at all, even though console doesn't show any error message and application didn't crashed. However after wrapping function of adding thread into try catch block i see that i had some stupid typo

 try:
            ThreadController.add_thread(process_func=MongoController.start_streaming_robot_state,
                                        close_func=MongoController.stop_streaming_robot_state)
        except Exception as e:
            logging.error(
                f"Error while starting robot state streaming: {str(e)}")
        

Upvotes: 0

Vinay Sajip
Vinay Sajip

Reputation: 99355

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.

Upvotes: 4

Related Questions