View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements. See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache license, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License. You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the license for the specific language governing permissions and
    15   * limitations under the license.
    16   */
    17  package org.apache.logging.log4j.taglib;
    18
    19  import javax.servlet.jsp.JspException;
    20  import javax.servlet.jsp.PageContext;
    21  import javax.servlet.jsp.tagext.BodyTagSupport;
    22  import javax.servlet.jsp.tagext.Tag;
    23
    24  import org.apache.logging.log4j.message.MessageFactory;
    25
    26  /**
    27   * This class implements the {@code <log:setLogger>} tag.
    28   *
    29   * @since 2.0
    30   */
    31  public class SetLoggerTag extends BodyTagSupport {
    32      private static final long serialVersionUID = 1L;
    33
    34      private transient Log4jTaglibLoggerContext loggerContext;
    35
    36      private transient Object logger;
    37
    38      private transient MessageFactory factory;
    39
    40      private String var;
    41
    42      private int scope;
    43
    44      public SetLoggerTag() {
    45          super();
    46          init();
    47      }
    48
    49      private void init() {
    50          this.logger = null;
    51          this.var = null;
    52          this.scope = PageContext.PAGE_SCOPE;
    53      }
    54
    55      @Override
    56      public void release() {
    57          super.release();
    58          this.init();
    59      }
    60
    61      @Override
    62      public void setPageContext(final PageContext pageContext) {
    63          super.setPageContext(pageContext);
    64          this.loggerContext = Log4jTaglibLoggerContext.getInstance(pageContext.getServletContext());
    65      }
    66
    67      public void setLogger(final Object logger) {
    68          this.logger = logger;
    69      }
    70
    71      public void setFactory(final MessageFactory factory) {
    72          this.factory = factory;
    73      }
    74
    75      public void setVar(final String var) {
    76          this.var = var;
    77      }
    78
    79      public void setScope(final String scope) {
    80          this.scope = TagUtils.getScope(scope);
    81      }
    82
    83      @Override
    84      public int doEndTag() throws JspException {
    85          final Log4jTaglibLogger logger = TagUtils.resolveLogger(this.loggerContext, this.logger, this.factory);
    86
    87          if (this.var != null) {
    88              this.pageContext.setAttribute(this.var, logger, this.scope);
    89          } else {
    90              TagUtils.setDefaultLogger(this.pageContext, logger);
    91          }
    92
    93          return Tag.EVAL_PAGE;
    94      }
    95  }