Logging in dotCMS offers significant control over your logging configuration using Apache log4j. To use dotCMS logging with your OSGi plugin, you must properly initialize and use dotCMS logging functions.
Note: If you fail to propertly initialize or call the dotCMS logging class, messages from your plugin may use the logging configuration from Tomcat instead of dotCMS (and may be logged in the Tomcat log file instead of the dotCMS log file).
Please read all of the following sections before implementing logging in your plugin:
Importing Logger Packages
The following package imports must be included in your plugin to use the dotCMS logging class:
import com.dotcms.repackage.org.apache.logging.log4j.LogManager;
import com.dotcms.repackage.org.apache.logging.log4j.core.LoggerContext;
import com.dotmarketing.loggers.Log4jUtil;
import com.dotmarketing.util.Logger;
Initializating the Logger
The following code example illustrates how to initialize your plugin log4j configuration so your plugin will use the dotCMS log4j configuration. You must add this initialization to the Activator
class in your plugin:
private LoggerContext pluginLoggerContext;
public void start ( BundleContext context ) throws Exception {
//Initializing log4j...
LoggerContext dotcmsLoggerContext = Log4jUtil.getLoggerContext();
//Initialing the log4j context of this plugin based on the dotCMS logger context
pluginLoggerContext = (LoggerContext) LogManager.getContext(this.getClass().getClassLoader(),
false,
dotcmsLoggerContext,
dotcmsLoggerContext.getConfigLocation());
//Initializing services...
initializeServices ( context );
...
Logging Messages
Once log4j has been initialized in your plugin, you may use the dotCMS Logger class anywhere inside your plugin.
com.dotmarketing.util.Logger.Logger.info(this.getClass().getName(), "Example");
Closing the Logger Context
To prevent performance and memory impacts, please make sure that you close the OSGi logger context when your plugin is stopped:
public void stop ( BundleContext context ) throws Exception {
unregisterServices( context );
//Shutting down log4j in order to avoid memory leaks
Log4jUtil.shutdown(pluginLoggerContext);
}
Complete Example
The following code demonstrates complete initialization, use, and closing of the logging context.
import com.dotcms.repackage.org.apache.logging.log4j.LogManager;
import com.dotcms.repackage.org.apache.logging.log4j.core.LoggerContext;
import com.dotcms.repackage.org.osgi.framework.BundleContext;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.loggers.Log4jUtil;
import com.dotmarketing.osgi.GenericBundleActivator;
import com.dotmarketing.portlets.contentlet.business.ContentletAPI;
import com.dotmarketing.util.Logger;
public class Activator extends GenericBundleActivator {
private LoggerContext pluginLoggerContext;
@SuppressWarnings ("unchecked")
public void start ( BundleContext context ) throws Exception {
//Initializing log4j...
LoggerContext dotcmsLoggerContext = Log4jUtil.getLoggerContext();
//Initialing the log4j context of this plugin based on the dotCMS logger context
pluginLoggerContext = (LoggerContext) LogManager.getContext(this.getClass().getClassLoader(),
false,
dotcmsLoggerContext,
dotcmsLoggerContext.getConfigLocation());
//Initializing services...
initializeServices ( context );
Long count = conAPI.contentletCount();
Logger.info(this, "+++++++++++++++++++++++++++++++++++++++++++++++");
Logger.info(this, "ContentletAPI.contentletCount() = " + count);
Logger.info(this, "+++++++++++++++++++++++++++++++++++++++++++++++");
}
public void stop ( BundleContext context ) throws Exception {
unregisterServices( context );
//Shutting down log4j in order to avoid memory leaks
Log4jUtil.shutdown(pluginLoggerContext);
}
}