    001    /*
    002     * Licensed to the Apache Software Foundation (ASF) under one or more
    003     * contributor license agreements. See the NOTICE file distributed with
    004     * this work for additional information regarding copyright ownership.
    005     * The ASF licenses this file to You under the Apache license, Version 2.0
    006     * (the "License"); you may not use this file except in compliance with
    007     * the License. You may obtain a copy of the License at
    008     *
    009     *      http://www.apache.org/licenses/LICENSE-2.0
    010     *
    011     * Unless required by applicable law or agreed to in writing, software
    012     * distributed under the License is distributed on an "AS IS" BASIS,
    013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014     * See the license for the specific language governing permissions and
    015     * limitations under the license.
    016     */
    017    package org.apache.logging.log4j.taglib;
    018
    019    import javax.servlet.jsp.JspException;
    020    import javax.servlet.jsp.tagext.Tag;
    021
    022    import org.apache.logging.log4j.Level;
    023
    024    /**
    025     * This class implements the {@code <log:catching>} tag.
    026     *
    027     * @since 2.0
    028     */
    029    public class CatchingTag extends ExceptionAwareTagSupport {
    030        private static final long serialVersionUID = 1L;
    031
    032        private static final String FQCN = CatchingTag.class.getName();
    033
    034        private Level level;
    035
    036        @Override
    037        protected void init() {
    038            super.init();
    039            this.level = null;
    040        }
    041
    042        public void setLevel(final Object level) {
    043            this.level = TagUtils.resolveLevel(level);
    044        }
    045
    046        @Override
    047        public int doEndTag() throws JspException {
    048            final Log4jTaglibLogger logger = this.getLogger();
    049
    050            if (this.level == null) {
    051                logger.catching(FQCN, Level.ERROR, this.getException());
    052            } else {
    053                logger.catching(FQCN, this.level, this.getException());
    054            }
    055
    056            return Tag.EVAL_PAGE;
    057        }
    058    }