Skip Navigation

Creating Access to ILoggerService

Before you begin, you need the following:

Complete either of the following procedures:

When you are done, read about the methods that you can use in the following topics. Add any methods that you want to your source code.

Inheriting from the AbstractEdgeBasePlugin Class

  1. In your Eclipse project, open the Dependencies tab.

  2. Add the following required dependencies to your plug-in:

    • com.sabre.edge.platform.core.logging

    • com.sabre.edge.platform.core.common

  1. Modify your Activator class to extend com.sabre.edge.platform.core.common.AbstractEdgeBasePlugin.

  2. In your source file for the logging service, retrieve the logging service by calling Activator.getDefault().getLoggerService() outside of your Activator class. The following code snippet shows this method.

ILoggerService logger = Activator.getDefault().getLoggerService();

Retrieving Access to ILoggerService Directly

  1. In your Eclipse project, open the Dependencies tab.

  2. Add the com.sabre.edge.platform.core.logging dependency to your plug-in.

  3. Modify the Activator class in your plug-in to obtain a new service, IloggerService.

The following sample Java code shows the modification to an Activator that obtains access to IloggerService directly.

package com.sabre.redapp.example.logger;

import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
import com.sabre.edge.platform.core.logging.ILoggerService;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin
{

    public static final String ID = "com.sabre.redapp.example.logger";

    private ILoggerService loggerService

    private static Activator plugin;

    /**
     * The constructor
     */
    public Activator()
    {
    }


    /**
     * {@inheritDoc}
     */
    public void start(BundleContext context) throws Exception
    {
        super.start(context);
        plugin = this;

        ServiceReference reference =  context.getServiceReference(ILoggerService.class.getName());
        loggerService = (ILoggerService)context.getService(reference);
        context.ungetService(reference);
    }

    /**
     * {@inheritDoc}
     */
    public void stop(BundleContext context) throws Exception
    {
        plugin = null;

        super.stop(context);
    }

    /**
     * Returns the shared instance
     *
     * @return the shared instance
     */
    public static Activator getDefault()
    {
        return plugin;
    }

    /**
     * Retrieves ILoggerService service
     * @return ILoggerService service
     */
    public ILoggerService getLoggerService()
    {
        return loggerService;
    }
}

 

  1. In your source file for the logging service, retrieve the logging service by calling Activator.getDefault().getLoggerService() outside of your Activator class. The following code shows this method.

ILoggerService logger = Activator.getDefault().getLoggerService();

Logging Messages

You can use a pre-defined method to log messages, or you can define logging manually in your logger source code.

You can add any of the following pre-defined methods to support logging levels and to log text messages:

severe(String msg)

warning(String msg)

info(String msg)

config(String msg)

fine(String msg)

finer(String msg)

finest(String msg)

The following sample code uses pre-defined methods to support log levels and log messages.

ILoggerService logger = Activator.getDefault().getLoggerService();

String message = "Logging message";

/* Logging a message on INFO level */
logger.info(message);

/* Logging a message on FINE level */
logger.fine(message);

/* Logging a message on WARNING level */
logger.warning(message);

 

You can define logging manually with the log() method.

The following sample code uses log() to define logging.

ILoggerService logger = Activator.getDefault().getLoggerService();

String message = "Logging message";

/* Logging a message on CONFIG level */
logger.log(Level.CONFIG, message);

/* Logging a message on SEVERE level */
logger.log(Level.SEVERE, message);

 

Logging Messages with an Exception

You can use any of the following pre-defined methods to log a message that is combined with an exception.

severe(String msg, Throwable thrown)

warning(String msg, Throwable thrown)

info(String msg, Throwable thrown)

config(String msg, Throwable thrown)

fine(String msg, Throwable thrown)

finer(String msg, Throwable thrown)

finest(String msg, Throwable thrown)

throwing(Throwable thrown)

The following sample code shows the use of pre-defined methods to log messages with an exception.

ILoggerService logger = Activator.getDefault().getLoggerService();

try
{
	String line = null;
	BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream("flights.txt")));

	while ((line = input.readLine()) != null)
	{
		/* more code here */
	}
}
catch (FileNotFoundException e)
{
	logger.warning("Could not find a file", e);
}
catch (IOException e)
{
	/* Logging an exception on SEVERE level */
	logger.throwing(e);
}

 

Using Log() with the IStatus Interface to Obtain the Outcome of an Operation

You can use the log() method that is in ILoggerService to obtain the outcome of an operation from an IStatus object. The following code uses this method.

log(String msg, IStatus status)

With this method, it is important to note that the IStatus severities depend on the log levels that you are capturing. The following list presents different IStatus severities with corresponding logging levels:

IStatus.OK is logged with the INFO level.

IStatus.INFO is logged with the `INFO`level.

IStatus.CANCEL is logged with the WARNING level.

IStatus.WARNING is logged with the `WARNING`level.

IStatus.ERROR is logged with the SEVERE level.

The following code demonstrates how to use the log() method with an IStatus object.

    Exception exception = new Exception("Database connection could not be established");

    IStatus errorStatus =
        new Status(IStatus.ERROR, Activator.ID, "Not able to store preference",
                   exception);

    /* Logging Eclispe IStatus on SEVERE level */
    logger.log("Could not store preferences in database", errorStatus);


    IStatus okStatus = new Status(IStatus.OK, Activator.ID, "Operation completed succesfully");

    /* Logging Eclipse IStatus on INFO level */
    logger.log("File was read successfully", okStatus);