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 java.util.ArrayList;
20  import java.util.List;
21  import javax.servlet.jsp.JspException;
22  import javax.servlet.jsp.tagext.BodyTag;
23  import javax.servlet.jsp.tagext.DynamicAttributes;
24  import javax.servlet.jsp.tagext.Tag;
25  
26  import org.apache.logging.log4j.Level;
27  import org.apache.logging.log4j.Marker;
28  import org.apache.logging.log4j.message.Message;
29  
30  /**
31   * Implements common methods for logging tags that accept messages and markers.
32   *
33   * @since 2.0
34   */
35  abstract class LoggingMessageTagSupport extends ExceptionAwareTagSupport implements DynamicAttributes {
36      private static final long serialVersionUID = 1L;
37  
38      private static final String FQCN = LoggingMessageTagSupport.class.getName();
39  
40      private transient Object message;
41  
42      private Marker marker;
43  
44      private List<Object> attributes;
45  
46      @Override
47      protected void init() {
48          super.init();
49          this.message = null;
50          this.marker = null;
51          if (this.attributes == null) {
52              this.attributes = new ArrayList<Object>();
53          } else {
54              this.attributes.clear();
55          }
56      }
57  
58      protected final Object getMessage() throws JspException {
59          if (this.message == null) {
60              if (this.getBodyContent() == null) {
61                  throw new JspException("Either message attribute or body content must be specified.");
62              }
63              return this.getBodyContent().getString();
64          }
65          return this.message;
66      }
67  
68      public final void setMessage(final Object message) {
69          this.message = message;
70      }
71  
72      protected final Marker getMarker() {
73          return this.marker;
74      }
75  
76      public final void setMarker(final Marker marker) {
77          this.marker = marker;
78      }
79  
80      protected abstract Level getLevel();
81  
82      @Override
83      public final void setDynamicAttribute(final String uri, final String name, final Object value) {
84          this.attributes.add(value);
85      }
86  
87      @Override
88      public final int doStartTag() {
89          return BodyTag.EVAL_BODY_BUFFERED;
90      }
91  
92      @Override
93      public final int doEndTag() throws JspException {
94          final Log4jTaglibLogger logger = this.getLogger();
95          final Level level = this.getLevel();
96          final Marker marker = this.getMarker();
97  
98          if (TagUtils.isEnabled(logger, level, marker)) {
99              final Object message = this.getMessage();
100             final Throwable exception = this.getException();
101             if (message instanceof Message) {
102                 logger.log(marker, FQCN, level, (Message) message, exception);
103             } else if (message instanceof String) {
104                 Message data;
105                 if (this.attributes.size() > 0) {
106                     data = logger.getMessageFactory().newMessage((String) message, this.attributes.toArray());
107                 } else {
108                     data = logger.getMessageFactory().newMessage((String) message);
109                 }
110                 logger.log(marker, FQCN, level, data, exception);
111             } else {
112                 logger.log(marker, FQCN, level, logger.getMessageFactory().newMessage(message), exception);
113             }
114         }
115 
116         return Tag.EVAL_PAGE;
117     }
118 }