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 org.apache.logging.log4j.Level;
20  import org.apache.logging.log4j.Logger;
21  import org.apache.logging.log4j.LoggingException;
22  import org.apache.logging.log4j.Marker;
23  import org.apache.logging.log4j.message.MessageFactory;
24  import org.apache.logging.log4j.spi.AbstractLogger;
25  import org.apache.logging.log4j.status.StatusLogger;
26  
27  import javax.servlet.jsp.JspException;
28  import javax.servlet.jsp.PageContext;
29  import java.util.HashSet;
30  import java.util.Set;
31  
32  /**
33   * Provides support for logging tags.
34   *
35   * @since 2.0
36   */
37  final class TagUtils {
38      private static final StatusLogger log = StatusLogger.getLogger();
39  
40      private static final Set<Object> WARNED_FOR = new HashSet<Object>();
41  
42      private static final String LOGGER_SCOPE_ATTRIBUTE = "org.apache.logging.log4j.taglib.LOGGER_SCOPE_ATTRIBUTE";
43  
44      private TagUtils() {
45          throw new RuntimeException("TagUtils cannot be instantiated.");
46      }
47  
48      static int getScope(String scope) {
49          if ("request".equalsIgnoreCase(scope)) {
50              return PageContext.REQUEST_SCOPE;
51          }
52          if ("session".equalsIgnoreCase(scope)) {
53              return PageContext.SESSION_SCOPE;
54          }
55          if ("application".equalsIgnoreCase(scope)) {
56              return PageContext.APPLICATION_SCOPE;
57          }
58          return PageContext.PAGE_SCOPE;
59      }
60  
61      static Level resolveLevel(Object level) {
62          if (level instanceof Level) {
63              return (Level) level;
64          }
65          if (level instanceof String) {
66              return Level.toLevel((String) level);
67          }
68          return null;
69      }
70  
71      static Log4jTaglibLogger resolveLogger(final Log4jTaglibLoggerContext context, final Object logger,
72                                             final MessageFactory factory) throws JspException {
73          if (logger instanceof Logger) {
74              if (logger instanceof Log4jTaglibLogger) {
75                  return (Log4jTaglibLogger) logger;
76              }
77              if (logger instanceof AbstractLogger) {
78                  if (log.isInfoEnabled() && !WARNED_FOR.contains(logger)) {
79                      log.info("Constructing new Log4jTaglibLogger from AbstractLogger {} name and message factory.",
80                              logger.getClass().getName());
81                      WARNED_FOR.add(logger);
82                  }
83                  AbstractLogger original = (AbstractLogger) logger;
84                  return getLogger(context, original.getName(), original.getMessageFactory());
85              }
86              throw new JspException(
87                      "Log4j Tag Library requires base logging system to extend Log4j AbstractLogger.");
88          }
89          if (logger instanceof String) {
90              return getLogger(context, (String) logger, factory);
91          }
92          throw new JspException("Logger must be of type String or org.apache.logging.log4j.Logger.");
93      }
94  
95      private static Log4jTaglibLogger getLogger(Log4jTaglibLoggerContext context, String name, MessageFactory factory)
96              throws JspException {
97          try {
98              return context.getLogger(name, factory);
99          } catch (LoggingException e) {
100             throw new JspException(e.getMessage(), e);
101         }
102     }
103 
104     static void setDefaultLogger(PageContext pageContext, Log4jTaglibLogger logger) {
105         pageContext.setAttribute(TagUtils.LOGGER_SCOPE_ATTRIBUTE, logger, PageContext.PAGE_SCOPE);
106     }
107 
108     static Log4jTaglibLogger getDefaultLogger(PageContext pageContext) {
109         return (Log4jTaglibLogger) pageContext.getAttribute(TagUtils.LOGGER_SCOPE_ATTRIBUTE, PageContext.PAGE_SCOPE);
110     }
111 
112     static boolean isEnabled(Log4jTaglibLogger logger, Level level, Marker marker) {
113         if (marker == null) {
114             return logger.isEnabled(level);
115         }
116         return logger.isEnabled(level, marker, (Object) null, null);
117     }
118 }