Coverage details for org.chwf.plugin.defaults.DefaultLogger

LineHitsSource
1 /*
2 Chrysalis Web Framework [http://chrysalis.sourceforge.net]
3 Copyright (c) 2002, 2003, 2004, Paul Strack
4  
5 All rights reserved.
6  
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9  
10 1. Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
12  
13 2. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16  
17 3. Neither the name of the copyright holder nor the names of its contributors
18 may be used to endorse or promote products derived from this software without
19 specific prior written permission.
20  
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
25 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
28 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32  
33 package org.chwf.plugin.defaults;
34  
35 import org.chwf.config.ConfigFactory;
36 import org.chwf.plugin.Logger;
37 import org.chwf.servlet.ServletData;
38  
39 /**
40  * A default user plugin implementation that uses the Servlet API to log errors
41  * and text. It ignores the context class, and uses the Chrysalis-Config.xml
42  * file to determine is debugging is active.
43  *
44  * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a>
45  */
461public class DefaultLogger extends Logger {
47  
48   /** Whether debug logging is enabled. */
49   private final boolean debug;
50  
51   /** Constuctor.<p> */
521  public DefaultLogger() {
531    this.debug =
54       ConfigFactory.getConfig(Logger.class).getBoolean("logger.debug", false);
551  }
56  
57   /**
58    * Log the message using the <code>ServletContext.log()</code> method or
59    * <code>System.err</code>, depending on what is available.<p>
60    *
61    * @param context The context class for the log message.
62    * @param message The message (string value is logged).
63    * @param ex The exception (logs the stack-trace).
64    * @param debug True if logged at the debug level.
65    */
66   public void log(Class context, Object message, Throwable ex, boolean debug) {
6710    if (!debug || this.debug) {
686      String msg = "[" + context.getName() + "] " + String.valueOf(message);
696      if (ServletData.isInitialized()) {
704        logToServletLog(ex, msg);
71       } else {
722        logToSystemErr(ex, msg);
73       }
74     }
7510  }
76  
77   /**
78    * Log the message using the <code>ServletContext.log()</code>.
79    *
80    * @param ex The exception (logs the stack-trace).
81    * @param msg The message (string value is logged).
82    */
83   private void logToServletLog(Throwable ex, String msg) {
844    if (ex == null) {
851      ServletData.getApplication().log(msg);
86     } else {
873      ServletData.getApplication().log(msg, ex);
88     }
894  }
90  
91   /**
92    * Log the message using System.err.
93    *
94    * @param ex The exception (logs the stack-trace).
95    * @param msg The message (string value is logged).
96    */
97   private void logToSystemErr(Throwable ex, String msg) {
982    System.err.println(msg);
992    if (ex != null) {
1001      ex.printStackTrace();
101     }
1022  }
103 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.