Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logger parameter for the LoggingEventHandler. #676

Merged
merged 3 commits into from
Jul 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Changelog

2020-xx-xx • `full history <https://github.com/gorakhargosh/watchdog/compare/v0.10.3...master>`__

- Add logger parameter for the LoggingEventHandler (`#676 <https://github.com/gorakhargosh/watchdog/pull/676>`_)
- Replace mutable default arguments with ``if None`` implementation (`#677 <https://github.com/gorakhargosh/watchdog/pull/677>`_)
- Thanks to our beloved contributors: @Sraw

Expand Down
15 changes: 10 additions & 5 deletions src/watchdog/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,30 +534,35 @@ def dispatch(self, event):
class LoggingEventHandler(FileSystemEventHandler):
"""Logs all the events captured."""

def __init__(self, logger=None):
super(LoggingEventHandler, self).__init__()

self.logger = logger or logging.root

def on_moved(self, event):
super(LoggingEventHandler, self).on_moved(event)

what = 'directory' if event.is_directory else 'file'
logging.info("Moved %s: from %s to %s", what, event.src_path,
event.dest_path)
self.logger.info("Moved %s: from %s to %s", what, event.src_path,
event.dest_path)

def on_created(self, event):
super(LoggingEventHandler, self).on_created(event)

what = 'directory' if event.is_directory else 'file'
logging.info("Created %s: %s", what, event.src_path)
self.logger.info("Created %s: %s", what, event.src_path)

def on_deleted(self, event):
super(LoggingEventHandler, self).on_deleted(event)

what = 'directory' if event.is_directory else 'file'
logging.info("Deleted %s: %s", what, event.src_path)
self.logger.info("Deleted %s: %s", what, event.src_path)

def on_modified(self, event):
super(LoggingEventHandler, self).on_modified(event)

what = 'directory' if event.is_directory else 'file'
logging.info("Modified %s: %s", what, event.src_path)
self.logger.info("Modified %s: %s", what, event.src_path)


class LoggingFileSystemEventHandler(LoggingEventHandler):
Expand Down