To enable logging, use either the ConsoleLogger or FileLogger class. There are three types of log 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 enable logging to sconsole, use the ConsoleLogger class as shown below:
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()
[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.
business-plan.pdf is converted file. Click here to download it.
Explanation
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: Write Logs to File
The following code demonstrates how to use FileLogger class and write logs to a text file:
fromgroupdocs.conversionimportConverter,ConverterSettingsfromgroupdocs.conversion.loggingimportFileLoggerfromgroupdocs.conversion.options.convertimportPdfConvertOptionsdefwrite_logs_to_file():# Set log file path and create loggerlog_file_path="./log.txt"file_logger=FileLogger(log_file_path)# Create converter settings and pass logger converter_settings=ConverterSettings()converter_settings.logger=file_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_file()
2024-11-04 12:15:05 [TRACE] Attempting to read the 'GROUPDOCS_LIC_PATH' environment variable for license file location.
2024-11-04 12:15:05 [TRACE] GroupDocs license has been set successfully.
2024-11-04 12:15:05 [TRACE] Determine loader for source document business-plan.docx ...
2024-11-04 12:15:05 [TRACE] ... loader selected.
2024-11-04 12:15:05 [TRACE] Loading source document business-plan.docx ...
2024-11-04 12:15:06 [TRACE] ... document loaded.
2024-11-04 12:15:06 [TRACE] Main document will be converted.
2024-11-04 12:15:06 [TRACE] Starting conversion of business-plan.docx...
2024-11-04 12:15:06 [TRACE] ... converter selected ...
2024-11-04 12:15:09 [TRACE] ... conversion completed.
business-plan.docx is the sample file used in this example. Click here to download it.
business-plan.pdf is converted file. Click here to download it.
Explanation
file_logger is created to facilitate logging. This logger writes log messages to the log.txt file.
converter_settings is instantiated, with its logger attribute set to the previously created file_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 written to the text file.
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.