1 16 package org.outerj.daisy.books.publisher.impl.util; 17 18 import org.outerj.daisy.books.store.BookInstance; 19 import org.outerj.daisy.books.publisher.impl.BookInstanceLayout; 20 import org.apache.commons.lang.exception.ExceptionUtils; 21 22 import java.io.OutputStream ; 23 import java.io.PrintWriter ; 24 import java.io.StringWriter ; 25 import java.text.DateFormat ; 26 import java.util.Locale ; 27 import java.util.Date ; 28 29 32 public class PublicationLog { 33 private PrintWriter pw; 34 private DateFormat dateFormat; 35 36 public PublicationLog(BookInstance bookInstance) throws Exception { 37 dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, Locale.US); 38 OutputStream os = bookInstance.getResourceOutputStream(BookInstanceLayout.getPublicationLogPath()); 39 pw = new PrintWriter (os); 40 } 41 42 public void dispose() { 43 if (pw != null) 44 pw.close(); 45 } 46 47 public void info(String message) { 48 output(message, "INFO "); 49 pw.flush(); 50 } 51 52 public void error(String message) { 53 error(message, null); 54 } 55 56 public void error(String message, Throwable t) { 57 output(message, "ERROR"); 58 if (t != null) 59 pw.println(getFullStackTrace(t)); 60 pw.flush(); 61 } 62 63 68 private String getFullStackTrace(Throwable throwable) { 69 StringWriter sw = new StringWriter (); 70 PrintWriter pw = new PrintWriter (sw, true); 71 Throwable [] ts = ExceptionUtils.getThrowables(throwable); 72 for (int i = 0; i < ts.length; i++) { 73 ts[i].printStackTrace(pw); 74 } 75 return sw.getBuffer().toString(); 76 } 77 78 private void output(String message, String type) { 79 String date = dateFormat.format(new Date ()); 80 pw.println("[" + type + "] <" + date + "> " + message); 81 } 82 } 83 | Popular Tags |