1 17 18 19 package org.apache.juli; 20 21 import java.io.File ; 22 import java.io.FileWriter ; 23 import java.io.PrintWriter ; 24 import java.sql.Timestamp ; 25 import java.util.logging.ErrorManager ; 26 import java.util.logging.Filter ; 27 import java.util.logging.Formatter ; 28 import java.util.logging.Handler ; 29 import java.util.logging.Level ; 30 import java.util.logging.LogManager ; 31 import java.util.logging.LogRecord ; 32 import java.util.logging.SimpleFormatter ; 33 34 41 42 public class FileHandler 43 extends Handler { 44 45 46 48 49 public FileHandler() { 50 configure(); 51 open(); 52 } 53 54 55 public FileHandler(String directory, String prefix, String suffix) { 56 this(); 57 this.directory = directory; 58 this.prefix = prefix; 59 this.suffix = suffix; 60 } 61 62 63 65 66 70 private String date = ""; 71 72 73 76 private String directory = null; 77 78 79 82 private String prefix = null; 83 84 85 88 private String suffix = null; 89 90 91 94 private PrintWriter writer = null; 95 96 97 99 100 105 public void publish(LogRecord record) { 106 107 if (!isLoggable(record)) { 108 return; 109 } 110 111 Timestamp ts = new Timestamp (System.currentTimeMillis()); 113 String tsString = ts.toString().substring(0, 19); 114 String tsDate = tsString.substring(0, 10); 115 116 if (!date.equals(tsDate)) { 118 synchronized (this) { 119 if (!date.equals(tsDate)) { 120 close(); 121 date = tsDate; 122 open(); 123 } 124 } 125 } 126 127 String result = null; 128 try { 129 result = getFormatter().format(record); 130 } catch (Exception e) { 131 reportError(null, e, ErrorManager.FORMAT_FAILURE); 132 return; 133 } 134 135 try { 136 writer.write(result); 137 writer.flush(); 138 } catch (Exception e) { 139 reportError(null, e, ErrorManager.WRITE_FAILURE); 140 return; 141 } 142 143 } 144 145 146 148 149 152 public void close() { 153 154 try { 155 if (writer == null) 156 return; 157 writer.write(getFormatter().getTail(this)); 158 writer.flush(); 159 writer.close(); 160 writer = null; 161 date = ""; 162 } catch (Exception e) { 163 reportError(null, e, ErrorManager.CLOSE_FAILURE); 164 } 165 166 } 167 168 169 172 public void flush() { 173 174 try { 175 writer.flush(); 176 } catch (Exception e) { 177 reportError(null, e, ErrorManager.FLUSH_FAILURE); 178 } 179 180 } 181 182 183 186 private void configure() { 187 188 Timestamp ts = new Timestamp (System.currentTimeMillis()); 189 String tsString = ts.toString().substring(0, 19); 190 date = tsString.substring(0, 10); 191 192 LogManager manager = LogManager.getLogManager(); 193 String className = FileHandler.class.getName(); 194 195 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 196 197 directory = getProperty(className + ".directory", "logs"); 199 prefix = getProperty(className + ".prefix", "juli."); 200 suffix = getProperty(className + ".suffix", ".log"); 201 202 setLevel(Level.parse(getProperty(className + ".level", "" + Level.ALL))); 204 205 String filterName = getProperty(className + ".filter", null); 207 if (filterName != null) { 208 try { 209 setFilter((Filter ) cl.loadClass(filterName).newInstance()); 210 } catch (Exception e) { 211 } 213 } 214 215 String formatterName = getProperty(className + ".formatter", null); 217 if (formatterName != null) { 218 try { 219 setFormatter((Formatter ) cl.loadClass(formatterName).newInstance()); 220 } catch (Exception e) { 221 } 223 } else { 224 setFormatter(new SimpleFormatter ()); 225 } 226 227 setErrorManager(new ErrorManager ()); 229 230 } 231 232 233 private String getProperty(String name, String defaultValue) { 234 String value = LogManager.getLogManager().getProperty(name); 235 if (value == null) { 236 value = defaultValue; 237 } else { 238 value = value.trim(); 239 } 240 return value; 241 } 242 243 244 247 private void open() { 248 249 File dir = new File (directory); 251 dir.mkdirs(); 252 253 try { 255 String pathname = dir.getAbsolutePath() + File.separator + 256 prefix + date + suffix; 257 writer = new PrintWriter (new FileWriter (pathname, true), true); 258 writer.write(getFormatter().getHead(this)); 259 } catch (Exception e) { 260 reportError(null, e, ErrorManager.OPEN_FAILURE); 261 writer = null; 262 } 263 264 } 265 266 267 } 268 | Popular Tags |