如何使用Python的watchdog API添加带有输出消息格式的“用户”部分?

2024-05-18 11:16:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要创建一个函数来记录对地图的访问。
代码如下所示:

import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler

def FileLogging():
    a = str(input("Give a directory you want to log (vb. D:\\\...\\\ExampleMap): "))
    print("To close the program press ctrl + c.")
    logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s', datefmt='%H:%M:%S')
    path = a
    event_handler = LoggingEventHandler()  
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

FileLogging()

我的问题是,我该如何做才能在format='%(asctime)s - %(message)s'中添加user

换句话说,我如何添加在地图/文件中更改内容的人

我试图在format='%(asctime)s - %(message)s'中添加%(user),但这不起作用,并给出错误:KeyError: 'user'

我现在得到的输出消息示例如下:

14:02:06 - Modified file: D:\\ExampleMap\\text1.txt

我希望它是这样的:

14:02:06 - Modified file: D:\\ExampleMap\\text1.txt - SliQ

Tags: fromimportformatmessagetimelogging地图observer