英文:
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'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, "\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?
答案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() / "Downloads")
Thread(target=start_watchdog_thread, args=(install_dir, downloads_dir, logger), daemon=True, name="Watchdog").start()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论