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.appserver.tomcat;
18  
19  import java.net.URI;
20  import java.net.URISyntaxException;
21  import java.net.URL;
22  
23  import org.apache.juli.logging.Log;
24  import org.apache.logging.log4j.Level;
25  import org.apache.logging.log4j.LogManager;
26  import org.apache.logging.log4j.spi.ExtendedLogger;
27  import org.apache.logging.log4j.spi.LoggerContext;
28  
29  /**
30   * Implements the Log interface from Tomcat 8.5 and greater.
31   *
32   * In order to use this class to cause Tomcat to use Log4j for logging, the jar containing this class as well as the
33   * log4j-api and log4j-core jars must be added to Tomcat's boot classpath. This is most easily accomplished by
34   * placing these jars in a directory and then adding the contents of that directory to the CLASSPATH
35   * environment variable in setenv.sh in Tomcat's bin directory.
36   *
37   * The Log4j configuration file must also be present on the classpath. This implementation will use the
38   * first file it finds with one of the following file names: log4j2-tomcat.xml, log4j2-tomcat.json,
39   * log4j2-tomcat.yaml, log4j2-tomcat.yml, log4j2-tomcat.properties. Again, this can be accomplished by adding
40   * this file to a directory and then adding that directory to the CLASSPATH environment variable in setenv.sh.
41   *
42   * @since 2.10.0
43   */
44  public class TomcatLogger implements Log {
45  
46      private static final long serialVersionUID = 1L;
47      private static final String FQCN = TomcatLogger.class.getName();
48      private static final String[] FILE_NAMES = {
49          "log4j2-tomcat.xml", "log4j2-tomcat.json", "log4j2-tomcat.yaml", "log4j2-tomcat.yml",
50          "log4j2-tomcat.properties"
51      };
52  
53      private final ExtendedLogger logger;
54  
55      /**
56       * This constructor is used by ServiceLoader to load an instance of the class.
57       */
58      public TomcatLogger() {
59          logger = null;
60      }
61  
62      /**
63       * This constructor is used by LogFactory to create a new Logger.
64       * @param name The name of the Logger.
65       */
66      public TomcatLogger(final String name) {
67          this.logger = PrivateManager.getLogger(name);
68      }
69  
70      @Override
71      public boolean isDebugEnabled() {
72          return logger.isDebugEnabled();
73      }
74  
75      @Override
76      public boolean isErrorEnabled() {
77          return logger.isErrorEnabled();
78      }
79  
80      @Override
81      public boolean isFatalEnabled() {
82          return logger.isFatalEnabled();
83      }
84  
85      @Override
86      public boolean isInfoEnabled() {
87          return logger.isInfoEnabled();
88      }
89  
90      @Override
91      public boolean isTraceEnabled() {
92          return logger.isTraceEnabled();
93      }
94  
95      @Override
96      public boolean isWarnEnabled() {
97          return logger.isWarnEnabled();
98      }
99  
100     @Override
101     public void trace(final Object o) {
102         logger.logIfEnabled(FQCN, Level.TRACE, null, o, null);
103     }
104 
105     @Override
106     public void trace(final Object o, final Throwable throwable) {
107         logger.logIfEnabled(FQCN, Level.TRACE, null, o, throwable);
108     }
109 
110     @Override
111     public void debug(final Object o) {
112         logger.logIfEnabled(FQCN, Level.DEBUG, null, o, null);
113     }
114 
115     @Override
116     public void debug(final Object o, final Throwable throwable) {
117         logger.logIfEnabled(FQCN, Level.DEBUG, null, o, throwable);
118     }
119 
120     @Override
121     public void info(final Object o) {
122         logger.logIfEnabled(FQCN, Level.INFO, null, o, null);
123     }
124 
125     @Override
126     public void info(final Object o, final Throwable throwable) {
127         logger.logIfEnabled(FQCN, Level.INFO, null, o, throwable);
128     }
129 
130     @Override
131     public void warn(final Object o) {
132         logger.logIfEnabled(FQCN, Level.WARN, null, o, null);
133     }
134 
135     @Override
136     public void warn(final Object o, final Throwable throwable) {
137         logger.logIfEnabled(FQCN, Level.WARN, null, o, throwable);
138     }
139 
140     @Override
141     public void error(final Object o) {
142         logger.logIfEnabled(FQCN, Level.ERROR, null, o, null);
143     }
144 
145     @Override
146     public void error(final Object o, final Throwable throwable) {
147         logger.logIfEnabled(FQCN, Level.ERROR, null, o, throwable);
148     }
149 
150     @Override
151     public void fatal(final Object o) {
152         logger.logIfEnabled(FQCN, Level.FATAL, null, o, null);
153     }
154 
155     @Override
156     public void fatal(final Object o, final Throwable throwable) {
157         logger.logIfEnabled(FQCN, Level.FATAL, null, o, throwable);
158     }
159 
160     /**
161      * Internal LogManager.
162      */
163     private static class PrivateManager extends LogManager {
164 
165         public static LoggerContext getContext() {
166             final ClassLoader cl = TomcatLogger.class.getClassLoader();
167             URI uri = null;
168             for (final String fileName : FILE_NAMES) {
169                 try {
170                     final URL url = cl.getResource(fileName);
171                     if (url != null) {
172                         uri = url.toURI();
173                         break;
174                     }
175                 } catch (final URISyntaxException ex) {
176                     // Ignore the exception.
177                 }
178             }
179             if (uri == null) {
180                 return getContext(FQCN, cl, false);
181             }
182             return getContext(FQCN, cl, false, uri, "Tomcat");
183         }
184 
185         public static ExtendedLogger getLogger(final String name) {
186             return getContext().getLogger(name);
187         }
188     }
189 }