Python Logging from Watchdog Thread

huangapple go评论71阅读模式
英文:

Python Logging from Watchdog Thread

问题

我正在编写一个脚本它在主线程中下载一些文件并启动一个看门狗线程使用[Watchdog](https://github.com/gorakhargosh/watchdog)库来监视下载文件夹并将它们移动到目标文件夹必须以这种方式完成我不能直接保存文件到目标文件夹)。

我的看门狗代码如下

``` python
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()

并且以如下方式运行:

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()
...
# 开始下载文件

然而,当我运行这个脚本时,我在控制台上看不到日志消息。我做错了什么?


<details>
<summary>英文:</summary>

I&#39;m writing a script which downloads some files in the main thread, and spins off a watchdog (using the [Watchdog](https://github.com/gorakhargosh/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:

``` python
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, &quot;\nWatchdog Running in {}/\n&quot;.format(self.directory))
        try:
            while True:
                time.sleep(1)
        except:
            self.observer.stop()
        self.observer.join()
        self.logger.log(logging.DEBUG, &quot;\nWatchdog Terminated\n&quot;)


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() / &quot;Downloads&quot;)
logger = logging.getLogger(LOGGER_NAME)
logger.setLevel(logging.DEBUG)
Thread(target=start_watchdog_thread, args=(install_dir, downloads_dir, logger), daemon=True, name=&quot;Watchdog&quot;).start()
...
#Start downloading files

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

答案1

得分: 1

问题在于记录器未配置为写入标准输出。我需要通过添加处理程序来正确配置它,如下所示:

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()
英文:

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() / &quot;Downloads&quot;)
Thread(target=start_watchdog_thread, args=(install_dir, downloads_dir, logger), daemon=True, name=&quot;Watchdog&quot;).start()

huangapple
  • 本文由 发表于 2023年4月4日 03:33:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923154.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定