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.
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.
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.
Example 3: Redirect ConsoleLogger Output to a File
If you already have a logging pipeline that consumes sys.stdout, you can redirect ConsoleLogger output there instead. Redirect sys.stdout for the duration of the render call — the logger will write its messages into the file handle:
importsysfromgroupdocs.viewerimportViewer,ViewerSettingsfromgroupdocs.viewer.loggingimportConsoleLoggerfromgroupdocs.viewer.optionsimportHtmlViewOptionsdefredirect_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 viewer settings with a console logger — the logger# writes through sys.stdout, which is currently redirected to log_file.viewer_settings=ViewerSettings(logger=ConsoleLogger())# Load DOCX document and render it to HTMLwithViewer("./sample.docx",settings=viewer_settings)asviewer:html_options=HtmlViewOptions.for_embedded_resources("redirect_logs_to_file/page_{0}.html")viewer.view(html_options)finally:sys.stdout=original_stdoutif__name__=="__main__":redirect_logs_to_file()
sample.docx is the sample file used in this example. Click here to download it.
sys.stdout is redirected to log.txt so any print-style writes (including the ones emitted by ConsoleLogger) land in the file.
ViewerSettings is instantiated with logger set to a fresh ConsoleLogger instance.
A Viewer object is created with the configured settings and performs rendering — 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.
Prefer FileLogger (Example 2) for new code — it avoids the global sys.stdout mutation. Use the redirection approach only when you need ConsoleLogger output to flow into an existing stdout-based pipeline.
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.