001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 * http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.atteo.moonshine.firebuglogger;
015
016import javax.xml.bind.annotation.XmlElement;
017import javax.xml.bind.annotation.XmlIDREF;
018import javax.xml.bind.annotation.XmlRootElement;
019
020import org.atteo.moonshine.TopLevelService;
021import org.atteo.moonshine.services.ImportService;
022import org.atteo.moonshine.webserver.ServletContainer;
023import org.slf4j.LoggerFactory;
024
025import com.google.inject.Module;
026import com.google.inject.PrivateModule;
027
028import ch.qos.logback.classic.Logger;
029import ch.qos.logback.classic.LoggerContext;
030import ch.qos.logback.classic.spi.ILoggingEvent;
031import ch.qos.logback.core.Appender;
032
033/**
034 * Supports logging to the Firebug console with FirePHP installed.
035 *
036 * <p>
037 * Download the extension from:<br/>
038 * https://addons.mozilla.org/en-US/firefox/addon/firephp/
039 * </p>
040 */
041@XmlRootElement(name = "firebugLogger")
042public class FireBugLoggerService extends TopLevelService {
043    public static final String FIRE_BUG_APPENDER = "FireBug Appender";
044
045    @XmlElement
046    @XmlIDREF
047    @ImportService
048    private ServletContainer servletContainer;
049
050    /**
051     * URL pattern specifying to which responses the headers will be added.
052     */
053    @XmlElement
054    private String pattern = "/*";
055
056    @Override
057    public Module configure() {
058        return new PrivateModule() {
059            @Override
060            protected void configure() {
061                bind(FireBugFilter.class);
062                servletContainer.addFilter(getProvider(FireBugFilter.class), pattern);
063            }
064        };
065    }
066
067    @Override
068    public void start() {
069        final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
070        final Logger rootLogger = context.getLogger(Logger.ROOT_LOGGER_NAME);
071
072        Appender<ILoggingEvent> appender = new FireBugAppender();
073        appender.setName(FIRE_BUG_APPENDER);
074        appender.setContext(context);
075        appender.start();
076
077        rootLogger.addAppender(appender);
078    }
079
080    @Override
081    public void close() {
082        final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
083        final Logger rootLogger = context.getLogger(Logger.ROOT_LOGGER_NAME);
084        rootLogger.detachAppender(FIRE_BUG_APPENDER);
085    }
086
087}