To enable logging, wire the built-in ConsoleLogger class through ConverterSettings when constructing the Converter. The logger emits three types of messages:
Error: for critical exceptions that prevent execution.
Warning: for recoverable or expected issues.
Trace: for general informational messages.
Example 1: Write Logs to Console
To stream log messages to standard output, instantiate ConsoleLogger, assign it to ConverterSettings.logger, and pass the settings to the Converter constructor:
fromgroupdocs.conversionimportConverter,ConverterSettingsfromgroupdocs.conversion.loggingimportConsoleLoggerfromgroupdocs.conversion.options.convertimportPdfConvertOptionsdefwrite_logs_to_console():# Create a console loggerconsole_logger=ConsoleLogger()# Create converter settings and pass logger converter_settings=ConverterSettings()converter_settings.logger=console_logger# Load DOCX document and convert it to PDFwithConverter("./business-plan.docx",converter_settings)asconverter:pdf_convert_options=PdfConvertOptions()converter.convert("./business-plan.pdf",pdf_convert_options)if__name__=="__main__":write_logs_to_console()
business-plan.docx is the sample file used in this example. Click here to download it.
console_logger is created to facilitate logging. This logger sends log messages to the console.
converter_settings is instantiated, with its logger attribute set to the previously created console_logger.
A Converter object is instantiated with the configured converter_settings.
The convert method is called and the conversion occurs, log messages are generated and displayed in the console.
Example 2: Redirect Logs to a File
The Python binding ships a single ConsoleLogger that writes to standard output. To persist the log stream to a text file, redirect sys.stdout for the duration of the conversion — the logger will then write its messages into the file handle:
importsysfromgroupdocs.conversionimportConverter,ConverterSettingsfromgroupdocs.conversion.loggingimportConsoleLoggerfromgroupdocs.conversion.options.convertimportPdfConvertOptionsdefwrite_logs_to_file():log_file_path="./log.txt"# Redirect standard output to a file so ConsoleLogger writes into itoriginal_stdout=sys.stdoutwithopen(log_file_path,"w",encoding="utf-8")aslog_file:sys.stdout=log_filetry:# Create converter settings and attach the console loggerconverter_settings=ConverterSettings()converter_settings.logger=ConsoleLogger()# Load DOCX document and convert it to PDFwithConverter("./business-plan.docx",converter_settings)asconverter:pdf_convert_options=PdfConvertOptions()converter.convert("./business-plan.pdf",pdf_convert_options)finally:sys.stdout=original_stdoutif__name__=="__main__":write_logs_to_file()
[TRACE] Attempting to read the 'GROUPDOCS_LIC_PATH' environment variable for license file location.
[TRACE] GroupDocs license has been set successfully.
[TRACE] Determine loader for source document business-plan.docx ...
[TRACE] ... loader selected.
[TRACE] Loading source document business-plan.docx ...
[TRACE] ... document loaded.
[TRACE] Main document will be converted.
[TRACE] Starting conversion of business-plan.docx...
[TRACE] ... converter selected ...
[TRACE] ... conversion completed.
business-plan.docx is the sample file used in this example. Click here to download it.
sys.stdout is redirected to a log.txt file so any print-style writes (including the ones emitted by ConsoleLogger) land in the file.
ConverterSettings is instantiated with logger set to a fresh ConsoleLogger instance.
A Converter object is created with the configured settings and performs the conversion — trace messages are captured in the file instead of the terminal.
sys.stdout is restored in the finally block so subsequent print calls continue to go to the terminal.
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.
On this page
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.