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

时间:2019-06-12 12:15:28

标签: python python-watchdog

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

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

我的问题是如何做到这一点,以便可以在user中添加format='%(asctime)s - %(message)s'

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

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

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

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

我想像这样:

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

1 个答案:

答案 0 :(得分:1)

无法得知是谁修改了文件,因此看门狗模块不支持该修改。