CridManager 2.0 - Logging service
Aus open7x0.org
Logging CridManager 2.0
Logging is one of the services provided by the service component. If you use logging in your component, make sure to get the ILoggingService interface injected in your component's constructor, either directly or via the ServiceContext object.
Use code like this:
public MyComponentConstructor(ILoggingService loggingservice)
{
logger = loggingservice.getLogger(this.getClass());
logger.info("Hi to you!");
}
The logging service simply provides you with a logger instance. The current implementation is here.
public interface ILoggingService {
public abstract ILogger getLogger(Class<?> clazz);
public abstract ILogger getLogger(String path);
}
If you need to pass the logging service to other classes in your component (you will need to), you may use a package visible variable (or getter method) for the logging service reference or you may pass the reference via the constructor of your other classes, whatever fits best. Do not pass the logger, always pass the logging service and receive an individual logger in other classes.
Example:
public MyComponentConstructor(ILoggingService loggingservice)
{
...
myUI = new MyComponentUI(loggingservice);
}
Always use the getLogger(Class<?> clazz) method of the logging service to get a logger to use in your class. The getLogger(String path) method should only be used for special purposes. For example, I use a logger getLogger("threads") for logging some special thread states across different classes.
Currently, log4j is used as the logging component.
This is reflected by the methods provided by the ILogger interface you get from the logging service.
public interface ILogger {
public abstract void debug(Object arg0, Throwable arg1);
public abstract void debug(Object arg0);
public abstract void error(Object arg0, Throwable arg1);
public abstract void error(Object arg0);
public abstract void fatal(Object arg0, Throwable arg1);
public abstract void fatal(Object arg0);
public abstract void info(Object arg0, Throwable arg1);
public abstract void info(Object arg0);
public abstract boolean isDebugEnabled();
public abstract boolean isInfoEnabled();
public abstract boolean isTraceEnabled();
public abstract void setLevel(Level arg0);
public abstract void trace(Object arg0, Throwable arg1);
public abstract void trace(Object arg0);
public abstract void warn(Object arg0, Throwable arg1);
public abstract void warn(Object arg0);
}
There are currently six debug levels, from low to high: trace, debug, info, warn, error and fatal.
Please refer to the log4j documentation for more help about the logger interface.
What and when to log
| This article about CridManager 2.0 is currently discussed |
| Please start or join the discussion |
The difficult part is to decide which information to log at which level.
The logging is not a debugging aid for the developer. If unit testing is adequately used, there should be no need for debug logging. So the main purpose is to get information what went wrong on the user's machine. This meens, you have to place the logging statements in advance and have to anticipate what may go wrong and what you need information about.
For tracing method invocations, please refer to the chapter below.

