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  
18  package org.apache.logging.log4j.jul;
19  
20  import java.util.logging.Filter;
21  import java.util.logging.Level;
22  import java.util.logging.LogRecord;
23  import java.util.logging.Logger;
24  
25  import org.apache.logging.log4j.message.Message;
26  import org.apache.logging.log4j.message.MessageFactory;
27  import org.apache.logging.log4j.spi.ExtendedLogger;
28  
29  /**
30   * Log4j API implementation of the JUL {@link Logger} class. <strong>Note that this implementation does
31   * <em>not</em> use the {@link java.util.logging.Handler} class.</strong> Instead, logging is delegated to the
32   * underlying Log4j {@link org.apache.logging.log4j.Logger} which may be implemented in one of many different ways.
33   * Consult the documentation for your Log4j Provider for more details.
34   * <p>Note that the methods {@link #getParent()} and {@link #setLevel(java.util.logging.Level)} are not supported by
35   * this implementation. If you need support for these methods, then you'll need to use log4j-core. The
36   * {@link #getParent()} method will not fail (thanks to JUL API limitations), but it won't necessarily be
37   * accurate!</p>
38   * <p>Also note that {@link #setParent(java.util.logging.Logger)} is explicitly unsupported. Parent loggers are
39   * determined using the syntax of the logger name; not through an arbitrary graph of loggers.</p>
40   *
41   * @since 2.1
42   */
43  public class ApiLogger extends Logger {
44  
45      private final WrappedLogger logger;
46      private static final String FQCN = ApiLogger.class.getName();
47  
48      ApiLogger(final ExtendedLogger logger) {
49          super(logger.getName(), null);
50          super.setLevel(LevelTranslator.toJavaLevel(logger.getLevel()));
51          this.logger = new WrappedLogger(logger);
52      }
53  
54      @Override
55      public void log(final LogRecord record) {
56          if (isFiltered(record)) {
57              return;
58          }
59          final org.apache.logging.log4j.Level level = LevelTranslator.toLevel(record.getLevel());
60          final Object[] parameters = record.getParameters();
61          final MessageFactory messageFactory = logger.getMessageFactory();
62          final Message message = parameters == null ?
63              messageFactory.newMessage(record.getMessage()) /* LOG4J2-1251: not formatted case */ :
64              messageFactory.newMessage(record.getMessage(), parameters);
65          final Throwable thrown = record.getThrown();
66          logger.logIfEnabled(FQCN, level, null, message, thrown);
67      }
68  
69      // support for Logger.getFilter()/Logger.setFilter()
70      boolean isFiltered(final LogRecord logRecord) {
71          final Filter filter = getFilter();
72          return filter != null && !filter.isLoggable(logRecord);
73      }
74  
75      @Override
76      public boolean isLoggable(final Level level) {
77          return logger.isEnabled(LevelTranslator.toLevel(level));
78      }
79  
80      @Override
81      public String getName() {
82          return logger.getName();
83      }
84  
85      @Override
86      public void setLevel(final Level newLevel) throws SecurityException {
87          throw new UnsupportedOperationException("Cannot set level through log4j-api");
88      }
89  
90      /**
91       * Provides access to {@link Logger#setLevel(java.util.logging.Level)}. This method should only be used by child
92       * classes.
93       *
94       * @see Logger#setLevel(java.util.logging.Level)
95       */
96      protected void doSetLevel(final Level newLevel) throws SecurityException {
97          super.setLevel(newLevel);
98      }
99  
100     /**
101      * Unsupported operation.
102      *
103      * @throws UnsupportedOperationException always
104      */
105     @Override
106     public void setParent(final Logger parent) {
107         throw new UnsupportedOperationException("Cannot set parent logger");
108     }
109 
110     @Override
111     public void log(final Level level, final String msg) {
112         logger.log(LevelTranslator.toLevel(level), msg);
113     }
114 
115     @Override
116     public void log(final Level level, final String msg, final Object param1) {
117         logger.log(LevelTranslator.toLevel(level), msg, param1);
118     }
119 
120     @Override
121     public void log(final Level level, final String msg, final Object[] params) {
122         logger.log(LevelTranslator.toLevel(level), msg, params);
123     }
124 
125     @Override
126     public void log(final Level level, final String msg, final Throwable thrown) {
127         logger.log(LevelTranslator.toLevel(level), msg, thrown);
128     }
129 
130     @Override
131     public void logp(final Level level, final String sourceClass, final String sourceMethod, final String msg) {
132         log(level, msg);
133     }
134 
135     @Override
136     public void logp(final Level level, final String sourceClass, final String sourceMethod, final String msg,
137                      final Object param1) {
138         log(level, msg, param1);
139     }
140 
141     @Override
142     public void logp(final Level level, final String sourceClass, final String sourceMethod, final String msg,
143                      final Object[] params) {
144         log(level, msg, params);
145     }
146 
147     @Override
148     public void logp(final Level level, final String sourceClass, final String sourceMethod, final String msg,
149                      final Throwable thrown) {
150         log(level, msg, thrown);
151     }
152 
153     @Override
154     public void logrb(final Level level, final String sourceClass, final String sourceMethod, final String bundleName,
155                       final String msg) {
156         log(level, msg);
157     }
158 
159     @Override
160     public void logrb(final Level level, final String sourceClass, final String sourceMethod, final String bundleName,
161                       final String msg, final Object param1) {
162         log(level, msg, param1);
163     }
164 
165     @Override
166     public void logrb(final Level level, final String sourceClass, final String sourceMethod, final String bundleName,
167                       final String msg, final Object[] params) {
168         log(level, msg, params);
169     }
170 
171     @Override
172     public void logrb(final Level level, final String sourceClass, final String sourceMethod, final String bundleName,
173                       final String msg, final Throwable thrown) {
174         log(level, msg, thrown);
175     }
176 
177     @Override
178     public void entering(final String sourceClass, final String sourceMethod) {
179         logger.entry();
180     }
181 
182     @Override
183     public void entering(final String sourceClass, final String sourceMethod, final Object param1) {
184         logger.entry(param1);
185     }
186 
187     @Override
188     public void entering(final String sourceClass, final String sourceMethod, final Object[] params) {
189         logger.entry(params);
190     }
191 
192     @Override
193     public void exiting(final String sourceClass, final String sourceMethod) {
194         logger.exit();
195     }
196 
197     @Override
198     public void exiting(final String sourceClass, final String sourceMethod, final Object result) {
199         logger.exit(result);
200     }
201 
202     @Override
203     public void throwing(final String sourceClass, final String sourceMethod, final Throwable thrown) {
204         logger.throwing(thrown);
205     }
206 
207     @Override
208     public void severe(final String msg) {
209         logger.logIfEnabled(FQCN, org.apache.logging.log4j.Level.ERROR, null, msg);
210     }
211 
212     @Override
213     public void warning(final String msg) {
214         logger.logIfEnabled(FQCN, org.apache.logging.log4j.Level.WARN, null, msg);
215     }
216 
217     @Override
218     public void info(final String msg) {
219         logger.logIfEnabled(FQCN, org.apache.logging.log4j.Level.INFO, null, msg);
220     }
221 
222     @Override
223     public void config(final String msg) {
224         logger.logIfEnabled(FQCN, LevelTranslator.CONFIG, null, msg);
225     }
226 
227     @Override
228     public void fine(final String msg) {
229         logger.logIfEnabled(FQCN, org.apache.logging.log4j.Level.DEBUG, null, msg);
230     }
231 
232     @Override
233     public void finer(final String msg) {
234         logger.logIfEnabled(FQCN, org.apache.logging.log4j.Level.TRACE, null, msg);
235     }
236 
237     @Override
238     public void finest(final String msg) {
239         logger.logIfEnabled(FQCN, LevelTranslator.FINEST, null, msg);
240     }
241 }