1 /* 2 * Copyright (c) 2009-2010 jMonkeyEngine 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 package com.jme3.util; 34 35 import java.io.PrintWriter; 36 import java.io.StringWriter; 37 import java.text.MessageFormat; 38 import java.util.Date; 39 import java.util.logging.Formatter; 40 import java.util.logging.LogRecord; 41 42 /** 43 * More simple formatter than the default one used in Java logging. 44 * Example output: <br/> 45 * INFO Display3D 12:00 PM: Display created. 46 */ 47 public class JmeFormatter extends Formatter { 48 49 private Date calendar = new Date(); 50 private String lineSeperator; 51 private MessageFormat format; 52 private Object args[] = new Object[1]; 53 private StringBuffer store = new StringBuffer(); 54 55 public JmeFormatter(){ 56 lineSeperator = System.getProperty("line.separator"); 57 format = new MessageFormat("{0,time}"); 58 } 59 60 @Override 61 public String format(LogRecord record) { 62 StringBuffer sb = new StringBuffer(); 63 64 calendar.setTime(record.getMillis()); 65 args[0] = calendar; 66 store.setLength(0); 67 format.format(args, store, null); 68 69 String clazz = null; 70 try{ 71 clazz = Class.forName(record.getSourceClassName()).getSimpleName(); 72 } catch (ClassNotFoundException ex){ 73 } 74 75 sb.append(record.getLevel().getLocalizedName()).append(" "); 76 sb.append(clazz).append(" "); 77 sb.append(store.toString()).append(" "); 78 sb.append(formatMessage(record)).append(lineSeperator); 79 80 if (record.getThrown() != null) { 81 try { 82 StringWriter sw = new StringWriter(); 83 PrintWriter pw = new PrintWriter(sw); 84 record.getThrown().printStackTrace(pw); 85 pw.close(); 86 sb.append(sw.toString()); 87 } catch (Exception ex) { 88 } 89 } 90 91 return sb.toString(); 92 } 93 } 94