loggertag.java

来自「jakarta-taglibs」· Java 代码 · 共 87 行

JAVA
87
字号
/*
 * Copyright 1999,2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.taglibs.log;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/** Abstract base class for the logging tags which log a message to a 
  * commons.logging category.
  *
  * @author <a href="mailto:joeo@epesh.com">Joseph Ottinger</a>
  * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
  */

public abstract class LoggerTag extends BodyTagSupport {
    
    private String category;
    private String message;

    
    public void setCategory(String category) {
        this.category = category;
    }
    
    public void setMessage(String message) {
        this.message = message;
    }
    
    // Tag interface
    //------------------------------------------------------------------------- 
    public int doStartTag() throws JspException  {
        if ( message != null ) {
            Log logCategory = getLoggingCategory();
            if ( isEnabled( logCategory ) ) {
                // Log now as doAfterBody() may not be called for an empty tag 
                log( logCategory, message );
            }
            return SKIP_BODY;
        }
        return EVAL_BODY_TAG;
    }
    
    public int doAfterBody() throws JspException {
        if (message == null) {
            Log logCategory = getLoggingCategory();
            if ( isEnabled( logCategory ) ) {
                log( logCategory, getBodyContent().getString().trim() );
            }
        }
        return SKIP_BODY;
    }
    
    // Implementation methods
    //------------------------------------------------------------------------- 
    protected abstract boolean isEnabled(Log logCategory);
    protected abstract void log(Log logCategory, String message);
    
    protected Log getLoggingCategory() {
        if ( category == null ) {
            // used to be Category.getRoot();
            return LogFactory.getLog("");
        }
        else {
            return LogFactory.getLog( category );
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?