1 package org.ashkelon.util; 2 6 7 import java.io.PrintWriter ; 8 9 17 public class Logger 18 { 19 public static final int ERROR = 10; 20 public static final int BRIEF = 20; 21 public static final int NORMAL = 30; 22 public static final int VERBOSE = 40; 23 public static final int FULL = 50; 24 public static final int DEBUG = 60; 25 26 private static Logger _instance = null; 27 private int traceLevel; 28 private PrintWriter writer, writer2; 29 private String prefix; 30 31 private Logger() 32 { 33 traceLevel = NORMAL; 34 setWriter(new PrintWriter (System.out)); 35 setWriter2(null); 36 setPrefix(""); 37 } 38 39 public static Logger getInstance() 40 { 41 if (_instance == null) 42 { 43 _instance = new Logger(); 44 } 45 return _instance; 46 } 47 48 51 public void setTraceLevel(int traceLevel) { this.traceLevel = traceLevel; } 52 public int getTraceLevel() { return traceLevel; } 53 54 public void setPrefix(String prefix) { this.prefix = StringUtils.avoidNull(prefix); } 55 public String getPrefix() { return prefix; } 56 57 60 public void setWriter(PrintWriter writer) 61 { 62 if (this.writer != null) 63 { 64 this.writer.close(); 67 } 70 this.writer = writer; 71 } 72 public PrintWriter getWriter() { return writer; } 73 74 78 public void setWriter2(PrintWriter writer2) 79 { 80 if (this.writer2 != null) 81 { 82 this.writer2.close(); 85 } 88 this.writer2 = writer2; 89 } 90 public PrintWriter getWriter2() { return writer2; } 91 92 95 public void trace(String text) 96 { 97 trace(text, traceLevel); 98 } 99 102 public void traceln(String text) 103 { 104 trace(text + "\n"); 105 } 106 107 110 public void trace(String text, int traceLevel) 111 { 112 if (traceLevel <= this.traceLevel) 113 { 114 if (!StringUtils.isBlank(prefix)) 115 { 116 text = prefix + ": " + text; 117 } 118 if (writer != null) 119 { 120 writer.print(text); 121 writer.flush(); 122 } 123 if (writer2 != null) 124 { 125 writer2.print(text); 126 writer2.flush(); 127 } 128 if (traceLevel==ERROR) 129 { System.err.print(text); 131 System.err.flush(); 132 } 133 134 } 135 } 136 137 140 public void traceln(String text, int traceLevel) 141 { 142 trace(text + "\n", traceLevel); 143 } 144 145 147 public void error(String text) 148 { 149 traceln(text, ERROR); 150 } 151 public void brief(String text) 152 { 153 traceln(text, BRIEF); 154 } 155 public void verbose(String text) 156 { 157 traceln(text, VERBOSE); 158 } 159 public void full(String text) 160 { 161 traceln(text, FULL); 162 } 163 public void debug(String text) 164 { 165 traceln(text, DEBUG); 166 } 167 168 169 } 170 | Popular Tags |