1 /* 2 Copyright (C) 2000 - 2007 Grid Systems, S.A. 3 4 This program is free software; you can redistribute it and/or modify 5 it under the terms of the GNU General Public License, version 2, as 6 published by the Free Software Foundation. 7 8 This program is distributed in the hope that it will be useful, 9 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License 14 along with this program; if not, write to the Free Software 15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 16 */ 17 18 /* 19 * Project: KernelConfigurator 20 * Created on 02-mar-2004 21 * 22 * Copyright (c)2003 Grid Systems 23 */ 24 package com.gridsystems.config.app; 25 26 import java.io.PrintWriter; 27 import java.io.StringWriter; 28 import java.text.MessageFormat; 29 import java.util.Date; 30 import java.util.logging.Formatter; 31 import java.util.logging.LogRecord; 32 33 /** 34 * Logging formatter with log4j-like style. 35 * 36 * @author <a href="mailto:rruiz@gridsystems.com">Rodrigo Ruiz Aguayo</a> 37 * @version 1.0 38 */ 39 public class PatternFormatter extends Formatter { 40 41 /** 42 * Line separator string. This is the value of the line.separator 43 * property at the moment that the SimpleFormatter was created. 44 */ 45 private String lineSeparator = System.getProperty("line.separator"); 46 47 /** 48 * The date to format. 49 */ 50 protected Date date = new Date(); 51 52 /** 53 * The message format arguments. 54 */ 55 protected Object[] args = new Object[3]; 56 57 /** 58 * The formatter to use. 59 */ 60 protected MessageFormat formatter = null; 61 62 /** 63 * The pattern. 64 */ 65 protected String pattern; 66 67 /** 68 * if true, the stack trace is dumped. 69 */ 70 protected boolean showTrace; 71 72 /** 73 * Creates an instance with a default one line pattern. 74 * <p> 75 * The default pattern is <code>{0,date,dd/MM/yyyy HH:mm:ss} {1} - {2}</code> 76 */ 77 public PatternFormatter() { 78 this("{0,date,dd/MM/yyyy HH:mm:ss} {1} - {2}", true); 79 } 80 81 /** 82 * Creates an instance that will use the specified pattern. The pattern will be 83 * used with three arguments: 84 * 85 * <ol> 86 * <li>java.util.Date with the message date and time 87 * <li>String with the message log level 88 * <li>String with the message to log 89 * </ol> 90 * 91 * If the message has an attached exception, its stacktrace will be presented only 92 * if the <code>showTrace</code> flag is set to true. 93 * 94 * @param pattern The pattern to use in this instance 95 * @param showTrace true if we want attached exceptions stack traces to be dumped 96 */ 97 public PatternFormatter(String pattern, boolean showTrace) { 98 this.pattern = pattern; 99 this.showTrace = showTrace; 100 } 101 102 /** 103 * {@inheritDoc} 104 */ 105 public String format(LogRecord record) { 106 if (formatter == null) { 107 formatter = new MessageFormat(pattern); 108 } 109 110 // Minimize memory allocations here. 111 date.setTime(record.getMillis()); 112 args[0] = date; 113 args[1] = record.getLevel().getLocalizedName(); 114 args[2] = formatMessage(record); 115 116 StringBuffer text = new StringBuffer(); 117 formatter.format(args, text, null); 118 119 text.append(lineSeparator); 120 if (showTrace && record.getThrown() != null) { 121 try { 122 StringWriter sw = new StringWriter(); 123 PrintWriter pw = new PrintWriter(sw); 124 record.getThrown().printStackTrace(pw); 125 pw.close(); 126 text.append(sw.toString()); 127 } catch (Exception ex) { } 128 } 129 return text.toString(); 130 } 131 }