Use advanced logging

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Redaction MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-redaction -- dnx GroupDocs.Redaction.Mcp --yes
Folders, licensing and more →

On this page

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)

Download full output

On this page