Skip to content

Commit cbe903f

Browse files
Merge pull request #18 from networktocode-llc/gfm-logging-improvements
Logging improvements
2 parents be67a12 + b2b90f0 commit cbe903f

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

dsync/__init__.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
ObjectNotFound,
3333
)
3434

35-
_logger = structlog.get_logger()
36-
3735

3836
class DSyncFlags(enum.Flag):
3937
"""Flags that can be passed to a sync_* or diff_* call to affect its behavior."""
@@ -57,6 +55,13 @@ class DSyncFlags(enum.Flag):
5755

5856
SKIP_UNMATCHED_BOTH = SKIP_UNMATCHED_SRC | SKIP_UNMATCHED_DST
5957

58+
LOG_UNCHANGED_RECORDS = enum.auto()
59+
"""If this flag is set, a log message will be generated during synchronization for each model, even unchanged ones.
60+
61+
By default, when this flag is unset, only models that have actual changes to synchronize will be logged.
62+
This flag is off by default to reduce the default verbosity of DSync, but can be enabled when debugging.
63+
"""
64+
6065

6166
class DSyncModel(BaseModel):
6267
"""Base class for all DSync object models.
@@ -339,7 +344,7 @@ def __init__(self, name=None):
339344
Subclasses should be careful to call super().__init__() if they override this method.
340345
"""
341346
self._data = defaultdict(dict)
342-
self._log = _logger.new(dsync=self)
347+
self._log = structlog.get_logger().new(dsync=self)
343348

344349
# If the type is not defined, use the name of the class as the default value
345350
if self.type is None:
@@ -454,6 +459,9 @@ def _sync_from_diff_element(
454459
raise ObjectNotDeleted(f"Failed to delete {object_class.get_type()} {element.keys} - not found!")
455460
obj = obj.delete()
456461
log.info("Deleted successfully", status="success")
462+
else:
463+
if flags & DSyncFlags.LOG_UNCHANGED_RECORDS:
464+
log.debug("No action needed", status="success")
457465
except ObjectCrudException as exception:
458466
log.error(str(exception), status="error")
459467
if not flags & DSyncFlags.CONTINUE_ON_FAILURE:

0 commit comments

Comments
 (0)