To enable logging, wire a logger through ViewerSettings when you construct the Viewer. GroupDocs.Viewer for Python via .NET ships two loggers out of the box — ConsoleLogger writes trace/warning/error messages to standard output, and FileLogger writes them directly to a log file. Both implement the same ILogger interface.
The logger emits three types of messages:
Error: for critical exceptions that prevent rendering.
Warning: for recoverable or expected issues.
Trace: for general informational messages.
Example 1: Write Logs to Console
To stream log messages to standard output, construct ViewerSettings with a ConsoleLogger and pass the settings to the Viewer constructor:
fromgroupdocs.viewerimportViewer,ViewerSettingsfromgroupdocs.viewer.loggingimportConsoleLoggerfromgroupdocs.viewer.optionsimportHtmlViewOptionsdefwrite_logs_to_console():# Create viewer settings with a console loggerviewer_settings=ViewerSettings(logger=ConsoleLogger())# Load DOCX document and render it to HTMLwithViewer("./sample.docx",settings=viewer_settings)asviewer:html_options=HtmlViewOptions.for_embedded_resources("write_logs_to_console/page_{0}.html")viewer.view(html_options)if__name__=="__main__":write_logs_to_console()
sample.docx is the sample file used in this example. Click here to download it.
[TRACE] Rendering into HTML with embedded resources.
[TRACE] Detecting file type by extension.
[TRACE] File type detected successfully: Microsoft Word Open XML Document.
[TRACE] Opening document.
[TRACE] Document is loaded.
[TRACE] Applying options.
[TRACE] Converting page 1.
[TRACE] Page 1 conversion completed.
[TRACE] Converting page 2.
[TRACE] Page 2 conversion completed.
[TRUNCATED]
ViewerSettings(logger=ConsoleLogger()) bundles a fresh console logger into a settings object. The logger keyword argument is the only construction form — ViewerSettings() with no arguments is not supported.
The Viewer is constructed with settings=viewer_settings. The logger is active for the whole with block.
As the view method is called and rendering occurs, trace / warning / error messages are written to standard output.
Example 2: Write Logs to a File with FileLogger
The Python binding ships a dedicated FileLogger class that writes directly to a log file — no stream redirection required. Pass the target file path to the constructor:
fromgroupdocs.viewerimportViewer,ViewerSettingsfromgroupdocs.viewer.loggingimportFileLoggerfromgroupdocs.viewer.optionsimportHtmlViewOptionsdefwrite_logs_to_file():# Create a file logger that writes trace/warning/error messages to log.txt# and wire it through ViewerSettingsviewer_settings=ViewerSettings(logger=FileLogger("./log.txt"))# Load DOCX document and render it to HTMLwithViewer("./sample.docx",settings=viewer_settings)asviewer:html_options=HtmlViewOptions.for_embedded_resources("write_logs_to_file/page_{0}.html")viewer.view(html_options)if__name__=="__main__":write_logs_to_file()
sample.docx is the sample file used in this example. Click here to download it.
[TRACE] Rendering into HTML with embedded resources.
[TRACE] Detecting file type by extension.
[TRACE] File type detected successfully: Microsoft Word Open XML Document.
[TRACE] Opening document.
[TRACE] Document is loaded.
[TRACE] Applying options.
[TRACE] Converting page 1.
[TRACE] Page 1 conversion completed.
[TRACE] Converting page 2.
[TRACE] Page 2 conversion completed.
[TRUNCATED]
FileLogger("./log.txt") opens log.txt for writing on first use and appends every trace, warning, and error message to it.
The file logger is passed as the logger keyword argument to ViewerSettings, and the settings object is then passed to the Viewer constructor.
All log messages produced during rendering land in log.txt — no need to redirect sys.stdout.
Use FileLogger when you want structured, per-run log files for long-running services or batch jobs.
Send console messages to a file
ConsoleLogger writes through the console of the .NET runtime, not through Python’s sys.stdout, so replacing sys.stdout (or using contextlib.redirect_stdout) does not capture its messages. To keep them in a file, use FileLogger (Example 2), or redirect the output of the script when you run it:
python write_logs_to_console.py > log.txt
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
Ask AI
Ask anything about this page — the answer is based on its content.
Searching the docs and writing an answer…
Something went wrong while getting an answer. Please try again in a moment.