-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_monitor.py
More file actions
73 lines (60 loc) · 2.38 KB
/
file_monitor.py
File metadata and controls
73 lines (60 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import time
import logging
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from datetime import datetime
import os
class FileChangeHandler(FileSystemEventHandler):
"""Handler for file system events"""
def __init__(self, log_file):
super().__init__()
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
filename=log_file,
filemode='a'
)
self.logger = logging.getLogger(__name__)
def on_created(self, event):
if not event.is_directory:
file_path = event.src_path
rel_path = os.path.relpath(file_path, start=os.path.dirname(os.path.abspath(__file__)))
self.logger.info(f"File Created: {rel_path}")
def on_modified(self, event):
if not event.is_directory:
file_path = event.src_path
rel_path = os.path.relpath(file_path, start=os.path.dirname(os.path.abspath(__file__)))
# Avoid logging modifications to the log file itself
if not rel_path.endswith('monitor.log'):
self.logger.info(f"File Modified: {rel_path}")
def on_deleted(self, event):
if not event.is_directory:
file_path = event.src_path
rel_path = os.path.relpath(file_path, start=os.path.dirname(os.path.abspath(__file__)))
self.logger.info(f"File Deleted: {rel_path}")
def monitor_directory(path_to_watch, log_file):
"""
Monitor a directory for changes and log events
Args:
path_to_watch (str): Directory path to monitor
log_file (str): Path to the log file
"""
event_handler = FileChangeHandler(log_file)
observer = Observer()
observer.schedule(event_handler, path_to_watch, recursive=True)
observer.start()
print(f"Started monitoring directory: {path_to_watch}")
print(f"Logging events to: {log_file}")
print("Press Ctrl+C to stop monitoring...")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
print("\nMonitoring stopped.")
observer.join()
if __name__ == "__main__":
current_dir = os.path.dirname(os.path.abspath(__file__))
log_file = os.path.join(current_dir, "monitor.log")
monitor_directory(current_dir, log_file)