Class
caplin.core.log

AbstractLogger

An abstract class to make it easy to build loggers.

It provides log level handling, and de-multiplexes all the method calls onto a single method that takes two parameters: the name of the method ('trace'/'debug'/'info', etc) and an array of the arguments passed to that method.

To use it, you can either extend this class and override the log method or pass another logger to delegate to into the constructor, or finally you can pass a log method directly into the constructor.

The caplin logging scheme allows the logData to have a template string as the first item. You can convert a logData into a string using the method this.logDataToString from inside your log method.

By default there is no data or log level information added into that string, so you will most probably want to prepend the time and level. Here is a simple example that logs to the console (although if you wish to do this, check out caplin.core.log.ConsoleLogger):

function MyLogger() {
	 caplin.core.log.AbstractLogger.call(this);
}
caplin.extend(MyLogger, caplin.core.log.AbstractLogger);
MyLogger.prototype.log = function(level, logData) {
	 console.log(new Date().toISOString(), level, this.logDataToString(logData));
};

Constructor Summary

Attributes Name and Description
caplin.core.log.AbstractLogger(Number level, Function delegate)

Constructs an AbstractLogger.

Field Summary

Attributes Name and Description
<static> caplin.core.log.AbstractLogger.DEBUG

Logs at a level suitable for developers.

<static> caplin.core.log.AbstractLogger.ERROR

Should only be used to log unrecoverable errors.

<static> caplin.core.log.AbstractLogger.INFO

Logs messages that could be useful information.

<static> caplin.core.log.AbstractLogger.TRACE

Logs at a very fine level, suitable for method entry / exits, etc.

<static> caplin.core.log.AbstractLogger.WARN

Logs messages that may indicate problems, but where the system is able to continue.

Method Summary

Attributes Name and Description
void log(String levelName, Array logData)

The abstract method that should be overridden by subclasses or by providing a delegate to the constructor.

String logDataToString(Array logData)

Turns an array of logging data into a string according to the caplin logging scheme.

void setLevel(Number level)

Changes the level at which this logger logs.

Methods inherited from class caplin.core.log.Logger:
debug, error, info, trace, warn

Constructor Detail

caplin.core.log.AbstractLogger(Number level, Function delegate)

Constructs an AbstractLogger.

Will usually be called in the super constructor of subclasses.

Parameters
Number level the level at which to log messages. Should be one of the static levels defined on this class. Will default to the most fine level if not provided.
Function delegate an optional function or other caplin.core.log.Logger to delegate logging to. Do not provide a delegate if you are sub-classing this class and overriding the log method.

Field Detail

<static> caplin.core.log.AbstractLogger.DEBUG

Logs at a level suitable for developers. May contain things like network level messages.

<static> caplin.core.log.AbstractLogger.ERROR

Should only be used to log unrecoverable errors. The application will probably not continue to function correctly after one of these errors. Messages at this level should be readable by non-developers.

<static> caplin.core.log.AbstractLogger.INFO

Logs messages that could be useful information. Messages at this level should be somewhat readable by non-developers.

<static> caplin.core.log.AbstractLogger.TRACE

Logs at a very fine level, suitable for method entry / exits, etc.

<static> caplin.core.log.AbstractLogger.WARN

Logs messages that may indicate problems, but where the system is able to continue. Messages at this level should be readable by non-developers.

Method Detail

void log(String levelName, Array logData)

The abstract method that should be overridden by subclasses or by providing a delegate to the constructor.

Parameters
String levelName the name of the method that was called.
Array logData the log information.

String logDataToString(Array logData)

Turns an array of logging data into a string according to the caplin logging scheme. See caplin.core.log.Log.interpolate for more information.

Parameters
Array logData an array containing all the data that should be logged.
Returns
A string representation of the logData provided.

void setLevel(Number level)

Changes the level at which this logger logs.

Parameters
Number level a log level, chosen from those available on this class.