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