0

I'm writing a script which downloads some files in the main thread, and spins off a watchdog (using the Watchdog library) thread to watch the downloads folder and move them into a target folder (It must be done this way, I cannot save the files directly into the target folder).

My watchdog looks like so:

class Watcher:

    def __init__(self, directory, handler, logger):
        self.observer = Observer()
        self.handler = handler
        self.directory = directory
        self.logger = logger

    def run(self):
        self.observer.schedule(
            self.handler, self.directory, recursive=True)
        self.observer.start()
        self.logger.log(logging.DEBUG, "\nWatchdog Running in {}/\n".format(self.directory))
        try:
            while True:
                time.sleep(1)
        except:
            self.observer.stop()
        self.observer.join()
        self.logger.log(logging.DEBUG, "\nWatchdog Terminated\n")


class DownloadHandler(FileSystemEventHandler):

    def __init__(self, target_dir, logger):
        super().__init__()
        self.target_dir = target_dir
        self.logger = logger
    
    def on_created(self, event):
        self.logger.log(logging.DEBUG, event.src_path)


def start_watchdog_thread(install_dir, downloads_dir, logger):
    Watcher(downloads_dir, DownloadHandler(install_dir, logger), logger).run()

And is being run as such:

install_dir = sys.argv[1]
downloads_dir = str(Path.home() / "Downloads")
logger = logging.getLogger(LOGGER_NAME)
logger.setLevel(logging.DEBUG)
Thread(target=start_watchdog_thread, args=(install_dir, downloads_dir, logger), daemon=True, name="Watchdog").start()
...
#Start downloading files

However, when I run this, I see no messages from the logger in the console. What have I done wrong?

Octa
  • 85
  • 6
  • Without seeing how your logger is configured, my guess is that the logger is not configured correctly to output to `stdout`. If you can share how your logger is configured (i.e., where you define the logger returned by `logging.getLogger(LOGGER_NAME)` I may be able to help more, but otherwise try looking here: https://stackoverflow.com/a/14058475/14717625 – Nicholas Barrow Apr 04 '23 at 15:34
  • 1
    @NicholasBarrow Thanks, that link contained the solution, I'll post an answer. – Octa Apr 04 '23 at 19:53

1 Answers1

1

The problem was that the logger was not configured to write to stdout. I needed to configure it properly by adding a handler like so:

logger = logging.getLogger(LOGGER_NAME)
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)

downloads_dir = str(Path.home() / "Downloads")
Thread(target=start_watchdog_thread, args=(install_dir, downloads_dir, logger), daemon=True, name="Watchdog").start()
Octa
  • 85
  • 6