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.slf4j;
18  
19  import org.apache.logging.log4j.message.MessageFactory;
20  import org.apache.logging.log4j.spi.ExtendedLogger;
21  import org.apache.logging.log4j.spi.LoggerContext;
22  import org.apache.logging.log4j.spi.LoggerRegistry;
23  import org.slf4j.LoggerFactory;
24  
25  /**
26   *
27   */
28  public class SLF4JLoggerContext implements LoggerContext {
29      private final LoggerRegistry<ExtendedLogger> loggerRegistry = new LoggerRegistry<>();
30  
31      @Override
32      public Object getExternalContext() {
33          return null;
34      }
35  
36      @Override
37      public ExtendedLogger getLogger(final String name) {
38          if (!loggerRegistry.hasLogger(name)) {
39              loggerRegistry.putIfAbsent(name, null, new SLF4JLogger(name, LoggerFactory.getLogger(name)));
40          }
41          return loggerRegistry.getLogger(name);
42      }
43  
44      @Override
45      public ExtendedLogger getLogger(final String name, final MessageFactory messageFactory) {
46          // FIXME according to LOG4J2-1180, the below line should be:
47          // FIXME if (!loggerRegistry.hasLogger(name, messageFactory)) {
48          if (!loggerRegistry.hasLogger(name)) {
49              // FIXME: should be loggerRegistry.putIfAbsent(name, messageFactory,
50              loggerRegistry.putIfAbsent(name, null,
51                      new SLF4JLogger(name, messageFactory, LoggerFactory.getLogger(name)));
52          }
53          // FIXME should be return loggerRegistry.getLogger(name, messageFactory);
54          return loggerRegistry.getLogger(name);
55  
56          // TODO applying the above fixes causes (log4j-to-slf4j) LoggerTest to fail
57      }
58  
59      @Override
60      public boolean hasLogger(final String name) {
61          return loggerRegistry.hasLogger(name);
62      }
63  
64      @Override
65      public boolean hasLogger(final String name, final MessageFactory messageFactory) {
66          return loggerRegistry.hasLogger(name, messageFactory);
67      }
68  
69      @Override
70      public boolean hasLogger(final String name, final Class<? extends MessageFactory> messageFactoryClass) {
71          return loggerRegistry.hasLogger(name, messageFactoryClass);
72      }
73  }