GroupDocs.Conversion provides logging capabilities to track the application’s behavior, debug issues, and record important events. You can use the built-in ConsoleLogger class to log to the console or implement a custom logger using the ILogger interface for other logging targets such as files, streams, or external systems.
Log Message Types
The log messages generated by the library are categorized as follows:
Error - for unrecoverable exceptions
Warning - for recoverable or expected exceptions.
Trace - for general application flow and informational messages.
Logging to the Console
The ConsoleLogger class is a ready-to-use implementation of ILogger that writes log messages to the standard console output.
importcom.groupdocs.conversion.Converter;importcom.groupdocs.conversion.ConverterSettings;importcom.groupdocs.conversion.logging.ConsoleLogger;importcom.groupdocs.conversion.options.convert.PdfConvertOptions;publicclassLogToConsole{publicstaticvoidconvert(){// Create ConsoleLogger instance
ConsoleLoggerlogger=newConsoleLogger();// Configure ConverterSettings with the logger
ConverterSettingssettingsFactory=newConverterSettings();settingsFactory.setLogger(logger);// Initialize the Converter with settings
try(Converterconverter=newConverter("formatting.docx",()->settingsFactory)){PdfConvertOptionsoptions=newPdfConvertOptions();converter.convert("converted.pdf",options);}}publicstaticvoidmain(String[]args){convert();}}
formatting.docx is sample file used in this example. Click here to download it.
[TRACE] Determine loaded for source document formatting.docx ...
[TRACE] ... loader selected.
[TRACE] Loading source document formatting.docx ...
[TRACE] ... document loaded.
[TRACE] Main document will be converted.
[TRACE] Starting conversion of formatting.docx...
[TRACE] ... pipeline selected ...
[TRACE] ... conversion completed.
Implementing custom logger
To log to a file or other destinations, you can create a custom logger by implementing the ILogger interface.
importcom.groupdocs.conversion.logging.ILogger;importjava.io.FileWriter;importjava.io.IOException;publicclassCustomFileLoggerimplementsILogger{privatefinalStringfileName;publicCustomFileLogger(StringfileName){this.fileName=fileName;}@Overridepublicvoidtrace(Stringmessage){writeToFile("[TRACE] "+message);}@Overridepublicvoidwarning(Stringmessage){writeToFile("[WARN] "+message);}@Overridepublicvoiderror(Stringmessage,Exceptionexception){writeToFile("[ERROR] "+message+", Exception: "+exception.getMessage());}privatevoidwriteToFile(Stringcontent){try(FileWriterwriter=newFileWriter(fileName,true)){writer.write(content+System.lineSeparator());}catch(IOExceptione){thrownewRuntimeException("Failed to write log message",e);}}}
importcom.groupdocs.conversion.Converter;importcom.groupdocs.conversion.ConverterSettings;importcom.groupdocs.conversion.logging.ILogger;importcom.groupdocs.conversion.options.convert.PdfConvertOptions;publicclassLogToCustom{publicstaticvoidconvert(){// Initialize custom file logger
CustomFileLoggerlogger=newCustomFileLogger("log.txt");// Configure ConverterSettings with the custom logger
ConverterSettingssettingsFactory=newConverterSettings();settingsFactory.setLogger(logger);// Initialize the Converter with settings
try(Converterconverter=newConverter("formatting.docx",()->settingsFactory)){PdfConvertOptionsoptions=newPdfConvertOptions();converter.convert("converted.pdf",options);}}publicstaticvoidmain(String[]args){convert();}}
formatting.docx is sample file used in this example. Click here to download it.
log.txt is output custom log file. Click here to download it.
This flexibility ensures you can monitor the application’s behavior and adapt logging to meet specific requirements.
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.