To enable logging, use the ConsoleLogger class or implement a custom logger via the ILogger interface.
The ILogger interface declares the methods used to perform logging.
The ConsoleLogger class implements the methods that output log messages to a standard console.
The log contains the following types of messages:
Error - for unrecoverable exceptions
Warning - for recoverable/expected exceptions
Trace - for general information
Logging to the console
To log to a standard console, use the ConsoleLogger class implementation:
StringdocumentPath="sample.docx";// NOTE: Put here actual path to your document
StringoutputPath="converted.pdf";// Create console logger
ConsoleLoggerlogger=newConsoleLogger();// Create ConverterSettings and specify logger.
ConverterSettingssettingsFactory=newConverterSettings();settingsFactory.setLogger(logger);Converterconverter=newConverter(documentPath,settingsFactory);PdfConvertOptionsoptions=newPdfConvertOptions();converter.convert(outputPath,options);
Console output would be as follows:
[TRACE] Determine loaded for source document sample.docx ...
[TRACE] ... loader selected.
[TRACE] Loading source document sample.docx ...
[TRACE] ... document loaded.
[TRACE] Main document will be converted.
[TRACE] Starting conversion of sample.docx...
[TRACE] ... pipeline selected ...
[TRACE] ... conversion completed.
Implementing custom logger
To log to a file, a stream or any other custom location, implement the ILogger interface:
For trace messages, implement the public void Trace(string message) method.
For warning messages, implement the public void Warning(string message) method.
For error messages, implement the public void Error(string message) method.
The following code snippet shows how to implement a simple file logger:
StringdocumentPath="sample.docx";// NOTE: Put here actual path to your document
StringoutputPath="converted.pdf";// Create logger and specify the output file
ILoggerlogger=newCustomFileLogger("log.txt");// Create ConverterSettings and specify logger.
ConverterSettingssettingsFactory=newConverterSettings();settingsFactory.setLogger(logger);Converterconverter=newConverter(documentPath,settingsFactory);PdfConvertOptionsoptions=newPdfConvertOptions();converter.convert(outputPath,options);publicclassCustomFileLogger:ILogger{privateString_fileName;/**
* Create logger to file.
* @param fileName Full file name with path
*/publicCustomFileLogger(StringfileName){_fileName=fileName;}/**
* Writes trace message to file.
* Trace log messages provide generally useful information about application flow.
* @param message The trace message.
*/publicvoidtrace(Stringmessage){StringcontentToAppend="[TRACE] "+message+System.lineSeparator();writer(contentToAppend);}/**
* Writes warning message to file.
* Warning log messages provide information about the unexpected and recoverable event in application flow.
* @param message The warning message.
*/publicvoidwarning(Stringmessage){StringcontentToAppend="[WARN] "+message+System.lineSeparator();writer(contentToAppend);}/**
* Writes an error message to file.
* Error log messages provide information about unrecoverable events in application flow.
* @param message The error message.
* @param exception The exception.
*/publicvoiderror(Stringmessage,com.groupdocs.conversion.internal.c.a.ms.System.Exceptionexception){StringcontentToAppend="[ERROR] "+message+", exception: "+exception+System.lineSeparator();writer(contentToAppend);}publicvoidwriter(Stringmessage){FileWriterfw=null;//the true will append the new data
try{fw=newFileWriter(_fileName,true);fw.write(message);//appends the string to the file
fw.close();}catch(IOExceptione){thrownewRuntimeException(e);}}}
The content of the log.txt file would be as follows:
[TRACE] Determine loaded for source document sample.docx ...
[TRACE] ... loader selected.
[TRACE] Loading source document sample.docx ...
[TRACE] ... document loaded.
[TRACE] Main document will be converted.
[TRACE] Starting conversion of sample.docx...
[TRACE] ... pipeline selected ...
[TRACE] ... conversion completed.
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.