Logging

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.

import com.groupdocs.conversion.Converter;
import com.groupdocs.conversion.ConverterSettings;
import com.groupdocs.conversion.logging.ConsoleLogger;
import com.groupdocs.conversion.options.convert.PdfConvertOptions;

public class LogToConsole {
    public static void convert() {
        // Create ConsoleLogger instance
        ConsoleLogger logger = new ConsoleLogger();

        // Configure ConverterSettings with the logger
        ConverterSettings settingsFactory = new ConverterSettings();
        settingsFactory.setLogger(logger);

        // Initialize the Converter with settings
        try (Converter converter = new Converter("formatting.docx", ()-> settingsFactory)) {
            PdfConvertOptions options = new PdfConvertOptions();
            converter.convert("converted.pdf", options);
        }
    }

    public static void main(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.

import com.groupdocs.conversion.logging.ILogger;
import java.io.FileWriter;
import java.io.IOException;

public class CustomFileLogger implements ILogger {

    private final String fileName;

    public CustomFileLogger(String fileName) {
        this.fileName = fileName;
    }

    @Override
    public void trace(String message) {
        writeToFile("[TRACE] " + message);
    }

    @Override
    public void warning(String message) {
        writeToFile("[WARN] " + message);
    }

    @Override
    public void error(String message, Exception exception) {
        writeToFile("[ERROR] " + message + ", Exception: " + exception.getMessage());
    }

    private void writeToFile(String content) {
        try (FileWriter writer = new FileWriter(fileName, true)) {
            writer.write(content + System.lineSeparator());
        } catch (IOException e) {
            throw new RuntimeException("Failed to write log message", e);
        }
    }
}
import com.groupdocs.conversion.Converter;
import com.groupdocs.conversion.ConverterSettings;
import com.groupdocs.conversion.logging.ILogger;
import com.groupdocs.conversion.options.convert.PdfConvertOptions;

public class LogToCustom {
    public static void convert() {
        // Initialize custom file logger
        CustomFileLogger logger = new CustomFileLogger("log.txt");

        // Configure ConverterSettings with the custom logger
        ConverterSettings settingsFactory = new ConverterSettings();
        settingsFactory.setLogger(logger);

        // Initialize the Converter with settings
        try (Converter converter = new Converter("formatting.docx", ()-> settingsFactory)) {
            PdfConvertOptions options = new PdfConvertOptions();
            converter.convert("converted.pdf", options);
        }
    }

    public static void main(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.