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.HashSet;
20  import java.util.Set;
21  import javax.servlet.jsp.JspException;
22  import javax.servlet.jsp.PageContext;
23  
24  import org.apache.logging.log4j.Level;
25  import org.apache.logging.log4j.Logger;
26  import org.apache.logging.log4j.LoggingException;
27  import org.apache.logging.log4j.Marker;
28  import org.apache.logging.log4j.message.MessageFactory;
29  import org.apache.logging.log4j.spi.AbstractLogger;
30  import org.apache.logging.log4j.status.StatusLogger;
31  
32  /**
33   * Provides support for logging tags.
34   *
35   * @since 2.0
36   */
37  final class TagUtils {
38      private static final StatusLogger LOGGER = 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(final 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(final 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 (LOGGER.isInfoEnabled() && !WARNED_FOR.contains(logger)) {
79                      LOGGER.info("Constructing new Log4jTaglibLogger from AbstractLogger {} name and message factory.",
80                              logger.getClass().getName());
81                      WARNED_FOR.add(logger);
82                  }
83                  final 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(final Log4jTaglibLoggerContext context, final String name,
96                                                 final MessageFactory factory)
97              throws JspException {
98          try {
99              return context.getLogger(name, factory);
100         } catch (final LoggingException e) {
101             throw new JspException(e.getMessage(), e);
102         }
103     }
104 
105     static void setDefaultLogger(final PageContext pageContext, final Log4jTaglibLogger logger) {
106         pageContext.setAttribute(TagUtils.LOGGER_SCOPE_ATTRIBUTE, logger, PageContext.PAGE_SCOPE);
107     }
108 
109     static Log4jTaglibLogger getDefaultLogger(final PageContext pageContext) {
110         return (Log4jTaglibLogger) pageContext.getAttribute(TagUtils.LOGGER_SCOPE_ATTRIBUTE, PageContext.PAGE_SCOPE);
111     }
112 
113     static boolean isEnabled(final Log4jTaglibLogger logger, final Level level, final Marker marker) {
114         if (marker == null) {
115             return logger.isEnabled(level);
116         }
117         return logger.isEnabled(level, marker, (Object) null, null);
118     }
119 }