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.Parser MCP server lets your agent work with files on your machine; they are never uploaded. It runs in Docker: replace /path/to/documents with the folder it may use.

claude mcp add groupdocs-parser -- docker run --rm -i -v /path/to/documents:/data ghcr.io/groupdocs-parser/parser-net-mcp:latest
Folders, licensing and more →

GroupDocs.Parser can report the errors, warnings and events that occur while it extracts data. Pass a logger to ParserSettings and the settings to the Parser constructor. The logger can be:

LoggerCalled as
A functionlog(level, message, exception), where level is "trace", "warning" or "error", and exception is the text of the .NET exception or None
An objecttrace(message), warning(message) and error(message, exception); a method the object does not define is skipped

Log with a function

Here are the steps to receive the information with a function:

  1. Define a function that takes level, message and exception;
  2. Create ParserSettings with the function;
  3. Instantiate the Parser object with the settings.
from groupdocs.parser import Parser
from groupdocs.parser.exceptions import InvalidPasswordException
from groupdocs.parser.options import ParserSettings


def log(level, message, exception):
    # level is "trace", "warning" or "error";
    # exception is the text of the .NET exception, or None
    print("%s: %s" % (level.capitalize(), message))


try:
    # Open a protected document without its password
    with Parser("./protected.pdf", ParserSettings(log)) as parser:
        print(parser.get_text())
except InvalidPasswordException:
    print("The document is password-protected.")

The following sample file is used in this example: protected.pdf

Error: Exception of type 'GroupDocs.Parser.Exceptions.InvalidPasswordException' was thrown.
The document is password-protected.

Download full output

Expected behavior: The logger reports the error before the exception reaches your code:

Error: Exception of type 'GroupDocs.Parser.Exceptions.InvalidPasswordException' was thrown.
The document is password-protected.

Log with an object

An object with trace, warning and error methods works too, the shape of the .NET ILogger interface:

from groupdocs.parser import Parser
from groupdocs.parser.exceptions import InvalidPasswordException
from groupdocs.parser.options import ParserSettings


class Logger:
    def trace(self, message):
        # Print event message
        print("Event: " + message)

    def warning(self, message):
        # Print warning message
        print("Warning: " + message)

    def error(self, message, exception):
        # Print error message
        print("Error: " + message)


try:
    with Parser("./protected.pdf", ParserSettings(Logger())) as parser:
        print(parser.get_text())
except InvalidPasswordException:
    pass  # The logger has already reported it

The following sample file is used in this example: protected.pdf

Error: Exception of type 'GroupDocs.Parser.Exceptions.InvalidPasswordException' was thrown.

Download full output

Expected behavior: Prints Error: Exception of type 'GroupDocs.Parser.Exceptions.InvalidPasswordException' was thrown.

Forward to Python’s logging module

Map the three levels onto the standard logging levels to route the engine’s messages to your application’s handlers:

import logging
import sys

from groupdocs.parser import Parser
from groupdocs.parser.exceptions import InvalidPasswordException
from groupdocs.parser.options import ParserSettings

# A handler of its own: logging.basicConfig() does nothing once the
# application has configured logging
logger = logging.getLogger("groupdocs.parser")
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(logging.Formatter("%(levelname)s %(name)s: %(message)s"))
logger.addHandler(handler)
LEVELS = {"trace": logging.DEBUG, "warning": logging.WARNING, "error": logging.ERROR}


def log(level, message, exception):
    logger.log(LEVELS.get(level, logging.INFO), message)


try:
    with Parser("./protected.pdf", ParserSettings(log)) as parser:
        parser.get_text()
except InvalidPasswordException:
    logger.info("Skipped a protected document")

The following sample file is used in this example: protected.pdf

ERROR groupdocs.parser: Exception of type 'GroupDocs.Parser.Exceptions.InvalidPasswordException' was thrown.
INFO groupdocs.parser: Skipped a protected document

Download full output

Expected behavior:

ERROR groupdocs.parser: Exception of type 'GroupDocs.Parser.Exceptions.InvalidPasswordException' was thrown.
INFO groupdocs.parser: Skipped a protected document

Notes

  • ParserSettings has no empty constructor: pass a logger, an OCR connector or an external resource handler. To pass a logger together with a handler, pass all three in order, with None for the OCR connector: ParserSettings(log, None, on_loading).
  • The engine reports what it encounters; a document that opens and parses cleanly may produce no messages at all.