View Javadoc
1 package org.apache.turbine.services.logging; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.io.ByteArrayOutputStream; 58 import java.io.FileWriter; 59 import java.io.IOException; 60 import java.io.PrintWriter; 61 import java.util.Date; 62 import java.util.Enumeration; 63 import java.util.Hashtable; 64 import java.util.Vector; 65 import org.apache.turbine.Turbine; 66 import org.apache.turbine.util.RunData; 67 68 /*** 69 * This class implements Logger interface using simple file 70 * writing. It handles only files and console as destinations. 71 * 72 * @see org.apache.turbine.services.logging.Logger 73 * @author <a href="mailto:Tomasz.Zielinski@e-point.pl">Tomasz Zielinski</a> 74 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a> 75 * @version $Id: FileLogger.java,v 1.3 2002/07/11 16:53:26 mpoeschl Exp $ 76 */ 77 public class FileLogger extends BaseLogger 78 { 79 /*** global table containing file writers */ 80 protected static Hashtable globalFilesTable; 81 82 /*** instance table containing file writers */ 83 protected Hashtable localFilesTable; 84 85 /*** line separator */ 86 protected String lf = System.getProperty("line.separator", "\n"); 87 88 /*** class initialization */ 89 static 90 { 91 globalFilesTable = new Hashtable(); 92 } 93 94 public FileLogger() 95 { 96 super(); 97 localFilesTable = new Hashtable(); 98 } 99 100 /*** Initialize and create the writers */ 101 public void init(LoggingConfig loggingConfig) 102 { 103 super.init(loggingConfig); 104 } 105 106 /*** 107 * Adds local file as destinations for logger. 108 * @param LoggingConfig configuration 109 */ 110 protected void configureFiles(LoggingConfig loggingConfig) 111 { 112 Vector files = loggingConfig.getFiles(); 113 for (Enumeration fileList = files.elements(); fileList.hasMoreElements(); ) 114 { 115 String path = (String) fileList.nextElement(); 116 117 // Resolve relative paths using Turbine.getRealPath(s) so that 118 // the paths are translated against the applicationRoot which 119 // may be the webContext or the CVS layout of a Turbine application. 120 String pathTmp = Turbine.getRealPath(path); 121 122 if (pathTmp != null) 123 { 124 path = pathTmp; 125 } 126 127 // check if the file is being used by another FileLogger instance. 128 // if so, use the writer from the global table to synchronize access 129 FileWriter writer = (FileWriter)globalFilesTable.get(path); 130 if (writer == null) 131 { 132 try 133 { 134 writer = new FileWriter(path, true); 135 globalFilesTable.put(path, writer); 136 } 137 catch(Exception e) 138 { 139 continue; 140 } 141 } 142 localFilesTable.put(path, writer); 143 } 144 } 145 146 /*** 147 * Adds console as a destination for logger. 148 * @param LoggingConfig configuration 149 */ 150 protected void configureConsole(LoggingConfig loggingConfig) 151 { 152 this.console = loggingConfig.getConsole(); 153 } 154 155 /*** 156 * It performs action that are need for deterimne whether 157 * logger was well configured or has any output 158 */ 159 public boolean checkLogger() 160 { 161 if (console) return true; 162 if (localFilesTable.size() > 0) return true; 163 return false; 164 } 165 166 /*** 167 * Also do a shutdown if the object is destroy()'d. 168 */ 169 protected void finalize() throws Throwable 170 { 171 shutdown(); 172 } 173 174 public void shutdown() 175 { 176 for (Enumeration files = localFilesTable.elements(); files.hasMoreElements(); ) 177 { 178 FileWriter fw = (FileWriter) files.nextElement(); 179 try 180 { 181 fw.close(); 182 } 183 catch (Exception e) 184 { 185 } 186 } 187 } 188 189 /*** 190 * This is a log metod with logLevel == DEBUG 191 */ 192 public void debug(String message) 193 { 194 log(DEBUG, message, null, null); 195 } 196 197 /*** 198 * This is a log metod with logLevel == DEBUG 199 */ 200 public void debug(String message, Throwable t) 201 { 202 log(DEBUG, message, null, t); 203 } 204 205 /*** 206 * This is a log metod with logLevel == DEBUG 207 */ 208 public void debug(String message, RunData data) 209 { 210 log(DEBUG, message, data, null); 211 } 212 213 /*** 214 * This is a log metod with logLevel == DEBUG 215 */ 216 public void debug(String message, RunData data, Throwable t) 217 { 218 log(DEBUG, message, data, t); 219 } 220 221 /*** 222 * This is a log metod with logLevel == INFO 223 */ 224 public void info(String message) 225 { 226 log(INFO, message, null, null); 227 228 } 229 230 /*** 231 * This is a log metod with logLevel == INFO 232 */ 233 public void info(String message, Throwable t) 234 { 235 log(INFO, message, null, t); 236 } 237 238 /*** 239 * This is a log metod with logLevel == INFO 240 */ 241 public void info(String message, RunData data) 242 { 243 log(INFO, message, data, null); 244 } 245 246 /*** 247 * This is a log metod with logLevel == INFO 248 */ 249 public void info(String message, RunData data, Throwable t) 250 { 251 log(INFO, message, data, t); 252 } 253 254 /*** 255 * This is a log metod with logLevel == WARN 256 */ 257 public void warn(String message) 258 { 259 log(WARN, message, null, null); 260 } 261 262 /*** 263 * This is a log metod with logLevel == WARN 264 */ 265 public void warn(String message, Throwable t) 266 { 267 log(WARN, message, null, t); 268 } 269 270 /*** 271 * This is a log metod with logLevel == WARN 272 */ 273 public void warn(String message, RunData data) 274 { 275 log(WARN, message, data, null); 276 } 277 278 /*** 279 * This is a log metod with logLevel == WARN 280 */ 281 public void warn(String message, RunData data, Throwable t) 282 { 283 log(WARN, message, data, t); 284 } 285 286 /*** 287 * This is a log metod with logLevel == ERROR 288 */ 289 public void error(String message) 290 { 291 log(ERROR, message, null, null); 292 } 293 294 public void error(String message, Throwable e) 295 { 296 log(ERROR, message, null, e); 297 } 298 299 public void error(Throwable e) 300 { 301 log(ERROR, null, null, e); 302 } 303 304 public void error(String message, RunData data) 305 { 306 log(ERROR, message, data, null); 307 } 308 309 public void error(String message, RunData data, Throwable e) 310 { 311 log(ERROR, message, data, e); 312 } 313 314 /*** 315 * Checks if logging is allowed and appends loglevel message 316 */ 317 protected void log(int level,String message, RunData data, Throwable e) 318 { 319 if (level < logLevel) 320 { 321 return; 322 } 323 String levelS; 324 switch (level) 325 { 326 case DEBUG: levelS = LEVELDEBUG; break; 327 case INFO: levelS = LEVELINFO; break; 328 case WARN: levelS = LEVELWARN; break; 329 case ERROR: levelS = LEVELERROR; break; 330 default: levelS = LEVELDEBUG; 331 } 332 333 logAll(levelS, message, data, e); 334 } 335 336 /*** log message to all open loging files */ 337 protected void logAll(String level,String description, RunData data, Throwable t) 338 { 339 340 Date date = new Date(); 341 StringBuffer logEntry = new StringBuffer(); 342 logEntry.append( "[" ); 343 logEntry.append( date.toString() ); 344 logEntry.append( "] -- " ); 345 logEntry.append( level ); 346 logEntry.append( " -- " ); 347 logEntry.append( description ); 348 if (data!=null) 349 { 350 logEntry.append( " -- " ); 351 logEntry.append(runDataFilter.getString(data)); 352 } 353 if (t != null) 354 { 355 logEntry.append ( lf ); 356 ByteArrayOutputStream ostr = new ByteArrayOutputStream(); 357 PrintWriter out = new PrintWriter(ostr, true); 358 out.write(logEntry.toString()); 359 out.write("\tException: "); 360 out.write(t.toString()); 361 out.write(lf+"\tStack Trace follows:" + lf + "\t"); 362 t.printStackTrace(out); 363 logEntry = new StringBuffer( ostr.toString() ); 364 } 365 if (console) 366 { 367 System.out.println(logEntry.toString()); 368 } 369 logEntry.append ( lf ); 370 371 for (Enumeration logFile = localFilesTable.elements(); logFile.hasMoreElements(); ) 372 { 373 FileWriter fw = (FileWriter)logFile.nextElement(); 374 synchronized( fw ) 375 { 376 try 377 { 378 fw.write( logEntry.toString(), 0, logEntry.length()); 379 fw.flush(); 380 } 381 catch(IOException e) 382 { 383 System.out.println("Cannot log message: " + e.getMessage()); 384 e.printStackTrace(); 385 } 386 } 387 } 388 } 389 }

This page was automatically generated by Maven