001 package org.hackystat.utilities.logger; 002 003 import java.text.SimpleDateFormat; 004 import java.util.Date; 005 import java.util.Locale; 006 import java.util.logging.Formatter; 007 import java.util.logging.LogRecord; 008 009 /** 010 * Provides a one line formatter for use with Hackystat logging. Supports optional date stamp 011 * prefix and optional appending of a newline. 012 * 013 * @author Philip Johnson 014 */ 015 public class OneLineFormatter extends Formatter { 016 017 /** Whether or not to include the date stamp in the format string. */ 018 private boolean enableDateStamp = true; 019 020 /** Whether or not to add a newline. */ 021 private boolean enableNewline = true; 022 023 /** 024 * Default constructor that enables the date stamp and new line. 025 */ 026 public OneLineFormatter () { 027 this(true, true); 028 } 029 030 /** 031 * One line format string with optional date stamp. Always adds a newline. 032 * @param enableDateStamp If true, a date stamp is inserted. 033 */ 034 public OneLineFormatter(boolean enableDateStamp) { 035 this(enableDateStamp, true); 036 } 037 038 /** 039 * One line format string with optional date stamp and optional newline. 040 * @param enableDateStamp If true, a date stamp is inserted. 041 * @param enableNewline If true, a newline is always inserted. 042 */ 043 public OneLineFormatter(boolean enableDateStamp, boolean enableNewline) { 044 this.enableDateStamp = enableDateStamp; 045 this.enableNewline = enableNewline; 046 } 047 048 /** 049 * Formats the passed log string as a single line. Prefixes the log string with a date stamp 050 * if enabled, and adds a newline if enabled. 051 * 052 * @param record A log record. 053 * @return The message string. 054 */ 055 @Override 056 public String format(LogRecord record) { 057 StringBuffer buff = new StringBuffer(); 058 if (this.enableDateStamp) { 059 SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd HH:mm:ss", Locale.US); 060 buff.append(dateFormat.format(new Date())); 061 buff.append(" "); 062 } 063 buff.append(record.getMessage()); 064 if (this.enableNewline) { 065 buff.append(System.getProperty("line.separator")); 066 } 067 return buff.toString(); 068 } 069 } 070 071 072