1 28 29 package com.caucho.log; 30 31 import com.caucho.util.L10N; 32 import com.caucho.vfs.WriteStream; 33 34 import java.util.logging.Formatter ; 35 import java.util.logging.Handler ; 36 import java.util.logging.LogRecord ; 37 38 41 public class StreamHandler extends Handler { 42 private static L10N L = new L10N(StreamHandler.class); 43 44 private WriteStream _os; 45 46 private Formatter _formatter; 47 48 private String _timestamp; 49 50 public StreamHandler(WriteStream os) 51 { 52 _os = os; 53 } 54 55 58 public void setTimestamp(String timestamp) 59 { 60 _timestamp = timestamp; 61 } 62 63 66 public void setFormatter(Formatter formatter) 67 { 68 _formatter = formatter; 69 } 70 71 74 public void publish(LogRecord record) 75 { 76 if (record.getLevel().intValue() < getLevel().intValue()) 77 return; 78 79 try { 80 if (record == null) { 81 synchronized (_os) { 82 _os.println("no record"); 83 _os.flush(); 84 } 85 return; 86 } 87 88 if (_formatter != null) { 89 String value = _formatter.format(record); 90 91 synchronized (_os) { 92 _os.println(value); 93 _os.flush(); 94 } 95 96 return; 97 } 98 99 String message = record.getMessage(); 100 Throwable thrown = record.getThrown(); 101 102 synchronized (_os) { 103 if (_timestamp != null) { 104 _os.print(_timestamp); 105 } 106 107 if (thrown != null) { 108 if (message != null && 109 ! message.equals(thrown.toString()) && 110 ! message.equals(thrown.getMessage())) 111 _os.println(message); 112 113 record.getThrown().printStackTrace(_os.getPrintWriter()); 114 } 115 else { 116 _os.println(record.getMessage()); 117 } 118 _os.flush(); 119 } 120 } catch (Throwable e) { 121 e.printStackTrace(); 122 } 123 } 124 125 128 public void flush() 129 { 130 } 131 132 135 public void close() 136 { 137 } 138 139 142 public int hashCode() 143 { 144 if (_os == null || _os.getPath() == null) 145 return super.hashCode(); 146 else 147 return _os.getPath().hashCode(); 148 } 149 150 153 public boolean equals(Object o) 154 { 155 if (this == o) 156 return true; 157 else if (getClass() != o.getClass()) 158 return false; 159 160 StreamHandler handler = (StreamHandler) o; 161 162 if (_os == null || handler._os == null) 163 return false; 164 else 165 return _os.getPath().equals(handler._os.getPath()); 166 } 167 168 public String toString() 169 { 170 if (_os == null) 171 return "StreamHandler@" + System.identityHashCode(this) + "[]"; 172 else 173 return ("StreamHandler@" + System.identityHashCode(this) 174 + "[" + _os.getPath() + "]"); 175 } 176 } 177 | Popular Tags |