KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > debug > LogTag


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.bridge.jsp.taglib.debug;
11
12 import javax.servlet.jsp.*;
13 import org.mmbase.bridge.jsp.taglib.ContextReferrerTag;
14 import javax.servlet.http.HttpServletRequest JavaDoc;
15
16 import org.mmbase.util.logging.Logger;
17 import org.mmbase.util.logging.Logging;
18
19 /**
20  * The implementation of the log tag.
21  *
22  * @author Michiel Meeuwissen
23  * @version $Id: LogTag.java,v 1.15 2006/08/30 18:02:16 michiel Exp $
24  */

25
26 public class LogTag extends ContextReferrerTag {
27     private Logger log;
28     private boolean doLog;
29     private int counter = 0; // A counter for every page. Because of this even <mm:log /> gets usefull.
30

31     public final static String JavaDoc LOGTAG_CATEGORY = Logging.PAGE_CATEGORY + ".LOGTAG"; // pages themselfs log to subcategories of this.
32

33     private String JavaDoc jspvar;
34     /**
35      * JspVar to Create, and write to
36      */

37     public void setJspvar(String JavaDoc j) {
38         jspvar = j;
39     }
40
41
42     public void setPageContext(PageContext pc) {
43         /* Determin only once per page if it can log */
44         super.setPageContext(pc);
45         log = (Logger) pageContext.getAttribute("__logtag_logger");
46         if(log == null) {
47             log = Logging.getLoggerInstance(LOGTAG_CATEGORY + ((HttpServletRequest JavaDoc)pageContext.getRequest()).getRequestURI().replace('/', '.'));
48             counter = 0;
49             pageContext.setAttribute("__logtag_logger", log);
50         }
51         doLog = log.isServiceEnabled();
52
53     }
54
55     public int doStartTag() throws JspTagException {
56         if (jspvar != null) {
57             pageContext.setAttribute(jspvar, log);
58             return EVAL_BODY;
59         } else if (doLog) {
60             return EVAL_BODY_BUFFERED;
61         } else {
62             return SKIP_BODY;
63         }
64     }
65
66     public int doEndTag() throws JspTagException {
67         if (doLog && jspvar == null) {
68             log.service(counter++ + ": " + (bodyContent != null ? bodyContent.getString() : "-"));
69         }
70         if (jspvar != null && EVAL_BODY == EVAL_BODY_BUFFERED) {
71
72             try {
73                 bodyContent.writeOut(bodyContent.getEnclosingWriter());
74             } catch (java.io.IOException JavaDoc e) {
75                 throw new JspTagException(e.toString());
76             }
77         }
78         super.doEndTag();
79         return EVAL_PAGE;
80     }
81 }
82
Popular Tags