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