Use advanced logging
Leave feedback
GroupDocs.Redaction reports what happens during a redaction to a logger, the Python side of the ILogger interface. A logger is any object with these three methods, each receiving a message:
error(message): an error that occurred during the redaction;warning(message): a warning;trace(message): an event, such as a redaction applied.
A method the object lacks is skipped. A function fn(level, message) also works as a logger, with level being "error", "warning" or "trace". A logger that raises is reported on stderr and never stops the redaction.
Pass the logger to RedactorSettings, the third argument of Redactor:
from groupdocs.redaction import Redactor
from groupdocs.redaction.options import LoadOptions, RedactorSettings
from groupdocs.redaction.redactions import DeleteAnnotationRedaction, ExactPhraseRedaction, ReplacementOptions
class CustomLogger(object):
"""Keeps every message, by level."""
def __init__(self):
self.errors = []
self.warnings = []
self.traces = []
def error(self, message):
self.errors.append(message)
def warning(self, message):
self.warnings.append(message)
def trace(self, message):
self.traces.append(message)
def use_advanced_logging():
logger = CustomLogger()
with Redactor("./sample.docx", LoadOptions(), RedactorSettings(logger)) as redactor:
redactor.apply(DeleteAnnotationRedaction())
redactor.apply(ExactPhraseRedaction("Emily Johnson", ReplacementOptions("[personal]")))
for message in logger.traces:
print("trace: " + message)
if not logger.errors:
result_path = redactor.save()
print("No errors. Output saved to " + result_path)
else:
print("Not saved, %d error(s): %s" % (len(logger.errors), logger.errors[0]))
if __name__ == "__main__":
use_advanced_logging()
sample.docx is the sample file used in this example. Click here to download it.
Binary file (PDF, 1.1 MB)
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.