1 22 23 29 package org.cofax.connectionpool; 30 31 import java.io.*; 32 import java.util.*; 33 34 public class LogWriter { 35 public static final int NONE = 0; 36 37 public static final int ERROR = 1; 38 39 public static final int INFO = 2; 40 41 public static final int DEBUG = 3; 42 43 private static final String ERROR_TEXT = "error"; 44 45 private static final String INFO_TEXT = "info"; 46 47 private static final String DEBUG_TEXT = "debug"; 48 49 private PrintWriter pw; 50 51 private String owner; 52 53 private int logLevel; 54 55 public LogWriter(String owner, int logLevel, PrintWriter pw) { 56 this.pw = pw; 57 this.owner = owner; 58 this.logLevel = logLevel; 59 } 60 61 public LogWriter(String owner, int logLevel) { 62 this(owner, logLevel, null); 63 } 64 65 public int getLogLevel() { 66 return logLevel; 67 } 68 69 public PrintWriter getPrintWriter() { 70 return pw; 71 } 72 73 public void setLogLevel(int logLevel) { 74 this.logLevel = logLevel; 75 } 76 77 public void setPrintWriter(PrintWriter pw) { 78 this.pw = pw; 79 } 80 81 public void log(String msg, int severityLevel) { 82 if (pw != null) { 83 if (severityLevel <= logLevel) { 84 pw.println("[" + new Date() + "] " + getSeverityString(severityLevel) + ": " + owner + ": " + msg); 85 } 86 } 87 } 88 89 public void log(Throwable t, String msg, int severityLevel) { 90 log(msg + " : " + toTrace(t), severityLevel); 91 } 92 93 private String getSeverityString(int severityLevel) { 94 switch (severityLevel) { 95 case ERROR: 96 return ERROR_TEXT; 97 case INFO: 98 return INFO_TEXT; 99 case DEBUG: 100 return DEBUG_TEXT; 101 default: 102 return "Unknown"; 103 } 104 } 105 106 private String toTrace(Throwable e) { 107 StringWriter sw = new StringWriter(); 108 PrintWriter pw = new PrintWriter(sw); 109 e.printStackTrace(pw); 110 pw.flush(); 111 return sw.toString(); 112 } 113 } 114 | Popular Tags |