Custom logging

By default logging is disabled when processing documents, but the product provides a way to pass your own logger implementation and use it while processing documents.

There is an interface that we can utilize:

  • ILogger - defines the interface for logging different process event like errors, warnings and information messages (traces).

There are 3 types of messages in the log file:

  • Error - for unrecoverable exceptions
  • Warning - for recoverable/expected/known exceptions
  • Trace - for general information

Implementing custom logger

To make your logger you should implement 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, Exception exception) method

Which of the three methods are called depends on the log level set with the setLogLevel method of SignatureSettings. See Set up logging.

In this example, we’ll implement a logger that writes messages in its own format. Replace the postMessage method to send them to a log file, a logging framework or an API endpoint.

// Create the custom logger
CustomLogger logger = new CustomLogger();

// Create SignatureSettings and specify the custom logger
SignatureSettings settings = new SignatureSettings(logger);

Signature signature = new Signature("sample.docx", settings);
try
{
    QrCodeSignOptions options = new QrCodeSignOptions("JohnSmith");
    // sign document to file
    signature.sign(outputFilePath, options);
}
finally
{
    signature.dispose();
}

/**
 * Writes log messages in a custom format. Replace postMessage to send them
 * to a log file, a logging framework or an API endpoint.
 */
public static class CustomLogger implements ILogger
{
    private final Object lock = new Object();

    /**
     * Error log messages provide information about unrecoverable events in application flow.
     */
    public void error(String message, Exception exception)
    {
        if (message == null)
            throw new IllegalArgumentException("message");
        if (exception == null)
            throw new IllegalArgumentException("exception");
        postMessage(LogLevel.Error, message + ". Exception: " + exception);
    }

    /**
     * Trace log messages provide generally useful information about application flow.
     */
    public void trace(String message)
    {
        if (message == null)
            throw new IllegalArgumentException("message");
        postMessage(LogLevel.Trace, message);
    }

    /**
     * Warning log messages provide information about the unexpected and recoverable event in application flow.
     */
    public void warning(String message)
    {
        if (message == null)
            throw new IllegalArgumentException("message");
        postMessage(LogLevel.Warning, message);
    }

    private void postMessage(int level, String message)
    {
        String header = "INFO";
        switch (level)
        {
            case LogLevel.Warning: header = "WARNING"; break;
            case LogLevel.Error: header = "ERROR"; break;
        }
        String date = new SimpleDateFormat("MM/dd/yyyy hh:mm a").format(new Date());
        String line = "GroupDocs.Signature " + header + " [" + date + "]. Message: " + message;
        synchronized (lock)
        {
            System.out.println(line);
        }
    }
}

The CustomLogging class of the Java examples uses this logger.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.