Skip to content

Commit

Permalink
Add logger parameter for the LoggingEventHandler (#676)
Browse files Browse the repository at this point in the history
* Add logger parameter for the LoggingEventHandler.

* update changelog.rst
  • Loading branch information
Sraw committed Jul 12, 2020
1 parent 4bd3cfe commit e985b67
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
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

0 comments on commit e985b67

Please sign in to comment.