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:
fromgroupdocs.redactionimportRedactorfromgroupdocs.redaction.optionsimportLoadOptions,RedactorSettingsfromgroupdocs.redaction.redactionsimportDeleteAnnotationRedaction,ExactPhraseRedaction,ReplacementOptionsclassCustomLogger(object):"""Keeps every message, by level."""def__init__(self):self.errors=[]self.warnings=[]self.traces=[]deferror(self,message):self.errors.append(message)defwarning(self,message):self.warnings.append(message)deftrace(self,message):self.traces.append(message)defuse_advanced_logging():logger=CustomLogger()withRedactor("./sample.docx",LoadOptions(),RedactorSettings(logger))asredactor:redactor.apply(DeleteAnnotationRedaction())redactor.apply(ExactPhraseRedaction("Emily Johnson",ReplacementOptions("[personal]")))formessageinlogger.traces:print("trace: "+message)ifnotlogger.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.