Jump to: navigation, search

Difference between revisions of "COSMOS Logging"

(Logging Configurations)
(Logging API)
Line 36: Line 36:
  
 
== Logging API ==
 
== Logging API ==
 +
In a java class that requires logging, get an instance of a logger with the following code:
 +
 +
<pre>
 +
import org.apache.log4j.Logger;
 +
Logger logger = Logger.getLogger(getClass());
 +
 +
String BUNDLE_NAME = "org.eclipse.cosmos.mydatamanager.messages";
 +
logger.setResourceBundle(ResourceBundle.getBundle(BUNDLE_NAME));
 +
</pre>
 +
 +
The logger can be made a private class variable. 
 +
 +
Log4j comes with some convenient methods for logging in different "levels" - the severity of log entry. 
 +
 +
This methods are:
 +
 +
<pre>
 +
public void debug(Object message);
 +
public void info(Object message);
 +
public void warn(Object message);
 +
public void error(Object message);
 +
public void fatal(Object message);
 +
 +
// the following methods will also include the stack trace of the Throwable t passed in as parameter.
 +
public void debug(Object message, Throwable t);
 +
public void info(Object message, Throwable t);
 +
public void warn(Object message, Throwable t);
 +
public void error(Object message, Throwable t);
 +
public void fatal(Object message, Throwable t);
 +
</pre>
 +
 +
log4j also provided more general logging API that supports localization:
 +
<pre>
 +
l7dlog(Priority priority, java.lang.String key, java.lang.Throwable t);
 +
l7dlog(Priority priority, java.lang.String key, java.lang.Object[] params, java.lang.Throwable t);
 +
</pre>
 +
 +
We will discuss the use of log levels and localization of log messages in a subsequent section.
 +
 
== Log Levels ==
 
== Log Levels ==
 
== Naming Convention for Message Keys ==
 
== Naming Convention for Message Keys ==

Revision as of 15:08, 13 May 2008

Logging for Web Applications and Web Services

Log4j will be used to do logging in web applications (such as the COSMOS UI) and web services (data managers). Information about log4j can be found at the log4j website.

Logging Configurations

To configure a web applciation to use log4j, we need a configuration file called log4j.properties in the classpath, usually put under WebContent/WEB-INF/classes. Axis2.jar comes with a log4j.properties that contains some default settings.

The installation/configuration program of the COSMOS demo should update the log4j.properties file to include settings appropriate for the COSMOS demo. Deployers can also customize the logging settings.

In order to log all log entries to the same log file, we need to add the following lines to the log4j.properties:

# Configure all classes in the org.eclipse.cosmos namespace to use the COSMOSLOG logger
# Change "ALL" to the desired log level
log4j.logger.org.eclipse.cosmos=ALL, COSMOSLOG

# COSMOSLOG is set to be a File appender using a PatternLayout.
log4j.appender.COSMOSLOG=org.apache.log4j.FileAppender
log4j.appender.COSMOSLOG.File=${CATALINA_HOME}/logs/cosmos.log
log4j.appender.COSMOSLOG.Append=true
log4j.appender.COSMOSLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.COSMOSLOG.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n

The above configuration will write the logs to cosmos.log file in the logs directory under the Tomcat install directory.

We can also configure log4j to open a log file for each web application or web service, assume each web application or web service has a unique package namespace. For example, if we want the broker to have a separate log file, we can add the following lines to the log4j.properties:

# Logger for COSMOS Broker
log4j.logger.org.eclipse.cosmos=ALL, BROKERLOG

# COSMOSLOG is set to be a File appender using a PatternLayout.
log4j.appender.BROKERLOG=org.apache.log4j.FileAppender
log4j.appender.BROKERLOG.File=${CATALINA_HOME}/logs/cosmos-broker.log
log4j.appender.BROKERLOG.Append=true
log4j.appender.BROKERLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.BROKERLOG.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n

Logging API

In a java class that requires logging, get an instance of a logger with the following code:

import org.apache.log4j.Logger;
Logger logger = Logger.getLogger(getClass());

String BUNDLE_NAME = "org.eclipse.cosmos.mydatamanager.messages";
logger.setResourceBundle(ResourceBundle.getBundle(BUNDLE_NAME));

The logger can be made a private class variable.

Log4j comes with some convenient methods for logging in different "levels" - the severity of log entry.

This methods are:

public void debug(Object message);
public void info(Object message);
public void warn(Object message);
public void error(Object message);
public void fatal(Object message);

// the following methods will also include the stack trace of the Throwable t passed in as parameter.
public void debug(Object message, Throwable t);
public void info(Object message, Throwable t);
public void warn(Object message, Throwable t);
public void error(Object message, Throwable t);
public void fatal(Object message, Throwable t);

log4j also provided more general logging API that supports localization:

l7dlog(Priority priority, java.lang.String key, java.lang.Throwable t);
l7dlog(Priority priority, java.lang.String key, java.lang.Object[] params, java.lang.Throwable t);

We will discuss the use of log levels and localization of log messages in a subsequent section.

Log Levels

Naming Convention for Message Keys

Externalize log messages

Logging for Eclipse Plugins