1 2 3 27 28 29 package org.apache.catalina.logger; 30 31 32 import java.io.File ; 33 import java.io.FileWriter ; 34 import java.io.IOException ; 35 import java.io.PrintWriter ; 36 import java.sql.Timestamp ; 37 import org.apache.catalina.LifecycleException; 38 import org.apache.catalina.util.LifecycleSupport; 39 import org.apache.catalina.util.StringManager; 40 41 42 50 51 public class FileLogger 52 extends LoggerBase { 53 54 55 57 58 62 private String date = ""; 63 64 65 68 private String directory = "logs"; 69 70 71 74 protected static final String info = 75 "org.apache.catalina.logger.FileLogger/1.0"; 76 77 78 81 private String prefix = "catalina."; 82 83 84 87 private StringManager sm = 88 StringManager.getManager(Constants.Package); 89 90 91 94 private boolean started = false; 95 96 97 100 private String suffix = ".log"; 101 102 103 106 private boolean timestamp = false; 107 108 109 112 private PrintWriter writer = null; 113 114 115 117 118 121 public String getDirectory() { 122 123 return (directory); 124 125 } 126 127 128 133 public void setDirectory(String directory) { 134 135 String oldDirectory = this.directory; 136 this.directory = directory; 137 support.firePropertyChange("directory", oldDirectory, this.directory); 138 139 } 140 141 142 145 public String getPrefix() { 146 147 return (prefix); 148 149 } 150 151 152 157 public void setPrefix(String prefix) { 158 159 String oldPrefix = this.prefix; 160 this.prefix = prefix; 161 support.firePropertyChange("prefix", oldPrefix, this.prefix); 162 163 } 164 165 166 169 public String getSuffix() { 170 171 return (suffix); 172 173 } 174 175 176 181 public void setSuffix(String suffix) { 182 183 String oldSuffix = this.suffix; 184 this.suffix = suffix; 185 support.firePropertyChange("suffix", oldSuffix, this.suffix); 186 187 } 188 189 190 193 public boolean getTimestamp() { 194 195 return (timestamp); 196 197 } 198 199 200 205 public void setTimestamp(boolean timestamp) { 206 207 boolean oldTimestamp = this.timestamp; 208 this.timestamp = timestamp; 209 support.firePropertyChange("timestamp", new Boolean (oldTimestamp), 210 new Boolean (this.timestamp)); 211 212 } 213 214 215 217 218 226 public void log(String msg) { 227 228 Timestamp ts = new Timestamp (System.currentTimeMillis()); 230 String tsString = ts.toString().substring(0, 19); 231 String tsDate = tsString.substring(0, 10); 232 233 if (!date.equals(tsDate)) { 235 synchronized (this) { 236 if (!date.equals(tsDate)) { 237 close(); 238 date = tsDate; 239 open(); 240 } 241 } 242 } 243 244 if (writer != null) { 246 if (timestamp) { 247 writer.println(tsString + " " + msg); 248 } else { 249 writer.println(msg); 250 } 251 } 252 253 } 254 255 256 258 259 262 private void close() { 263 264 if (writer == null) 265 return; 266 writer.flush(); 267 writer.close(); 268 writer = null; 269 date = ""; 270 271 } 272 273 274 277 private void open() { 278 279 File dir = new File (directory); 281 if (!dir.isAbsolute()) 282 dir = new File (System.getProperty("catalina.base"), directory); 283 dir.mkdirs(); 284 285 try { 287 String pathname = dir.getAbsolutePath() + File.separator + 288 prefix + date + suffix; 289 writer = new PrintWriter (new FileWriter (pathname, true), true); 290 } catch (IOException e) { 291 writer = null; 292 } 293 294 } 295 296 297 299 300 308 public void start() throws LifecycleException { 309 310 if (started) 312 throw new LifecycleException 313 (sm.getString("fileLogger.alreadyStarted")); 314 lifecycle.fireLifecycleEvent(START_EVENT, null); 315 started = true; 316 317 super.start(); 318 319 } 320 321 322 330 public void stop() throws LifecycleException { 331 332 if (!started) 334 throw new LifecycleException 335 (sm.getString("fileLogger.notStarted")); 336 lifecycle.fireLifecycleEvent(STOP_EVENT, null); 337 started = false; 338 339 close(); 340 341 super.stop(); 342 343 } 344 345 346 } 347 | Popular Tags |