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:
Logger
Called as
A function
log(level, message, exception), where level is "trace", "warning" or "error", and exception is the text of the .NET exception or None
An object
trace(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:
Define a function that takes level, message and exception;
Create ParserSettings with the function;
Instantiate the Parser object with the settings.
fromgroupdocs.parserimportParserfromgroupdocs.parser.exceptionsimportInvalidPasswordExceptionfromgroupdocs.parser.optionsimportParserSettingsdeflog(level,message,exception):# level is "trace", "warning" or "error";# exception is the text of the .NET exception, or Noneprint("%s: %s"%(level.capitalize(),message))try:# Open a protected document without its passwordwithParser("./protected.pdf",ParserSettings(log))asparser:print(parser.get_text())exceptInvalidPasswordException:print("The document is password-protected.")
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
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:
importloggingimportsysfromgroupdocs.parserimportParserfromgroupdocs.parser.exceptionsimportInvalidPasswordExceptionfromgroupdocs.parser.optionsimportParserSettingslogging.basicConfig(stream=sys.stdout,level=logging.DEBUG,format="%(levelname)s%(name)s: %(message)s")logger=logging.getLogger("groupdocs.parser")LEVELS={"trace":logging.DEBUG,"warning":logging.WARNING,"error":logging.ERROR}deflog(level,message,exception):logger.log(LEVELS.get(level,logging.INFO),message)try:withParser("./protected.pdf",ParserSettings(log))asparser:parser.get_text()exceptInvalidPasswordException:logger.info("Skipped a protected document")
The following sample file is used in this example: protected.pdf
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.