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