View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.internal.impl.slf4j;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import org.eclipse.aether.spi.locator.Service;
26  import org.eclipse.aether.spi.locator.ServiceLocator;
27  import org.eclipse.aether.spi.log.Logger;
28  import org.eclipse.aether.spi.log.LoggerFactory;
29  import org.eclipse.sisu.Nullable;
30  import org.slf4j.ILoggerFactory;
31  import org.slf4j.spi.LocationAwareLogger;
32  
33  /**
34   * A logger factory that delegates to <a href="http://www.slf4j.org/" target="_blank">SLF4J</a> logging.
35   *
36   * @deprecated Use SLF4J instead
37   */
38  @Singleton
39  @Named("slf4j")
40  @Deprecated
41  public class Slf4jLoggerFactory implements LoggerFactory, Service {
42  
43      private static final boolean AVAILABLE;
44  
45      static {
46          boolean available;
47          try {
48              Slf4jLoggerFactory.class.getClassLoader().loadClass("org.slf4j.ILoggerFactory");
49              available = true;
50          } catch (Exception | LinkageError e) {
51              available = false;
52          }
53          AVAILABLE = available;
54      }
55  
56      public static boolean isSlf4jAvailable() {
57          return AVAILABLE;
58      }
59  
60      private ILoggerFactory factory;
61  
62      /**
63       * Creates an instance of this logger factory.
64       */
65      public Slf4jLoggerFactory() {
66          // enables no-arg constructor
67      }
68  
69      @Inject
70      Slf4jLoggerFactory(@Nullable ILoggerFactory factory) {
71          setLoggerFactory(factory);
72      }
73  
74      public void initService(ServiceLocator locator) {
75          setLoggerFactory(locator.getService(ILoggerFactory.class));
76      }
77  
78      public Slf4jLoggerFactory setLoggerFactory(ILoggerFactory factory) {
79          this.factory = factory;
80          return this;
81      }
82  
83      public Logger getLogger(String name) {
84          org.slf4j.Logger logger = getFactory().getLogger(name);
85          if (logger instanceof LocationAwareLogger) {
86              return new Slf4jLoggerEx((LocationAwareLogger) logger);
87          }
88          return new Slf4jLogger(logger);
89      }
90  
91      private ILoggerFactory getFactory() {
92          if (factory == null) {
93              factory = org.slf4j.LoggerFactory.getILoggerFactory();
94          }
95          return factory;
96      }
97  
98      private static final class Slf4jLogger implements Logger {
99  
100         private final org.slf4j.Logger logger;
101 
102         Slf4jLogger(org.slf4j.Logger logger) {
103             this.logger = logger;
104         }
105 
106         public boolean isDebugEnabled() {
107             return logger.isDebugEnabled();
108         }
109 
110         public void debug(String msg) {
111             logger.debug(msg);
112         }
113 
114         public void debug(String msg, Throwable error) {
115             logger.debug(msg, error);
116         }
117 
118         public boolean isWarnEnabled() {
119             return logger.isWarnEnabled();
120         }
121 
122         public void warn(String msg) {
123             logger.warn(msg);
124         }
125 
126         public void warn(String msg, Throwable error) {
127             logger.warn(msg, error);
128         }
129     }
130 
131     private static final class Slf4jLoggerEx implements Logger {
132 
133         private static final String FQCN = Slf4jLoggerEx.class.getName();
134 
135         private final LocationAwareLogger logger;
136 
137         Slf4jLoggerEx(LocationAwareLogger logger) {
138             this.logger = logger;
139         }
140 
141         public boolean isDebugEnabled() {
142             return logger.isDebugEnabled();
143         }
144 
145         public void debug(String msg) {
146             logger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, msg, null, null);
147         }
148 
149         public void debug(String msg, Throwable error) {
150             logger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, msg, null, error);
151         }
152 
153         public boolean isWarnEnabled() {
154             return logger.isWarnEnabled();
155         }
156 
157         public void warn(String msg) {
158             logger.log(null, FQCN, LocationAwareLogger.WARN_INT, msg, null, null);
159         }
160 
161         public void warn(String msg, Throwable error) {
162             logger.log(null, FQCN, LocationAwareLogger.WARN_INT, msg, null, error);
163         }
164     }
165 }