package org.jzonic.jlo.formatter;
import org.jzonic.jlo.LogRecord;
import org.jzonic.jlo.formatter.tokens.TokenParser;
import java.util.Map;
/**
* This formatter can be configured using the format parameter. Syntax
* <pre>
* <formatter class="DefinedFormatter">
* <parameter name="format" value="${date} ${time} : [${target}] [${class}] ${text}"/>
* </formatter>
* </pre>
* The class uses the TokenParser to extract the tokens from the format string and
* the TokenParser will replace every occurence with the specific output of the
* FormatterToken
*
* @author Andreas Mecky
* @author Terry Dye
*/
public class DefinedFormatter extends AbstractFormatter {
private String format = "${date} ${time} : [${target}] [${class}] ${text}";
public DefinedFormatter(String configName) {
super(configName);
}
/**
* Formats the LogRecord according to the defined format
*
* @param lr the LogRecord
* @return the formatted String
*/
public String formatMessage(LogRecord lr) {
TokenParser tp = new TokenParser();
return tp.parseLine(lr,format);
}
/**
* Sets the format that should be used to render the LogRecord. If not set
* the default format is:<br/>
* ${date} ${time} : [${target}] [${class}] ${text}
*
* @param params a Map containing all parameters defined for the formatter
*/
public void setParameter(Map params) {
if ( params.containsKey("format")) {
format = (String)params.get("format");
}
}
}