001package org.eclipse.aether.internal.impl.slf4j;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import javax.inject.Inject;
023import javax.inject.Named;
024import javax.inject.Singleton;
025
026import org.eclipse.aether.spi.locator.Service;
027import org.eclipse.aether.spi.locator.ServiceLocator;
028import org.eclipse.aether.spi.log.Logger;
029import org.eclipse.aether.spi.log.LoggerFactory;
030import org.eclipse.sisu.Nullable;
031import org.slf4j.ILoggerFactory;
032import org.slf4j.spi.LocationAwareLogger;
033
034/**
035 * A logger factory that delegates to <a href="http://www.slf4j.org/" target="_blank">SLF4J</a> logging.
036 *
037 * @deprecated Use SLF4J instead
038 */
039@Singleton
040@Named( "slf4j" )
041@Deprecated
042public class Slf4jLoggerFactory
043    implements LoggerFactory, Service
044{
045
046    private static final boolean AVAILABLE;
047
048    static
049    {
050        boolean available;
051        try
052        {
053            Slf4jLoggerFactory.class.getClassLoader().loadClass( "org.slf4j.ILoggerFactory" );
054            available = true;
055        }
056        catch ( Exception | LinkageError e )
057        {
058            available = false;
059        }
060        AVAILABLE = available;
061    }
062
063    public static boolean isSlf4jAvailable()
064    {
065        return AVAILABLE;
066    }
067
068    private ILoggerFactory factory;
069
070    /**
071     * Creates an instance of this logger factory.
072     */
073    public Slf4jLoggerFactory()
074    {
075        // enables no-arg constructor
076    }
077
078    @Inject
079    Slf4jLoggerFactory( @Nullable ILoggerFactory factory )
080    {
081        setLoggerFactory( factory );
082    }
083
084    public void initService( ServiceLocator locator )
085    {
086        setLoggerFactory( locator.getService( ILoggerFactory.class ) );
087    }
088
089    public Slf4jLoggerFactory setLoggerFactory( ILoggerFactory factory )
090    {
091        this.factory = factory;
092        return this;
093    }
094
095    public Logger getLogger( String name )
096    {
097        org.slf4j.Logger logger = getFactory().getLogger( name );
098        if ( logger instanceof LocationAwareLogger )
099        {
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}