1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.logging.logback.internal

File LogbackEventGenerator.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

6
24
8
1
177
100
13
0.54
3
8
1.62

Classes

Class Line # Actions
LogbackEventGenerator 67 24 0% 13 3
0.9210526392.1%
 

Contributing tests

This file is covered by 162 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.logging.logback.internal;
21   
22    import java.util.Collections;
23    import java.util.List;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Singleton;
28   
29    import org.slf4j.Logger;
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.component.manager.ComponentLifecycleException;
32    import org.xwiki.component.manager.ComponentLookupException;
33    import org.xwiki.component.manager.ComponentManager;
34    import org.xwiki.component.phase.Disposable;
35    import org.xwiki.component.phase.Initializable;
36    import org.xwiki.component.phase.InitializationException;
37    import org.xwiki.logging.LogLevel;
38    import org.xwiki.logging.LogUtils;
39    import org.xwiki.logging.event.LogEvent;
40    import org.xwiki.observation.EventListener;
41    import org.xwiki.observation.ObservationManager;
42    import org.xwiki.observation.event.Event;
43   
44    import ch.qos.logback.classic.spi.ILoggingEvent;
45    import ch.qos.logback.classic.spi.IThrowableProxy;
46    import ch.qos.logback.classic.spi.ThrowableProxy;
47    import ch.qos.logback.core.AppenderBase;
48   
49    /**
50    * Bridge converting log to Observation Events.
51    * <p>
52    * Note that this class is implemented as an Event Listener only because we needed a way for this component to be
53    * initialized early when the system starts and the Observation Manager Component is the first Component loaded in the
54    * system and in its own initialization it initializes all Event Listeners... The reason we want this component
55    * initialized early is because it adds itself as a Logback Appender in its initialization and thus by having it done
56    * early any other component wishing to listen to logs will be able to do so and not "loose" events (there's still a
57    * possibility that some logs will not be seen if some Event Listeners do logging in their initialization and it happens
58    * that they're initialized before this component...).
59    * </p>
60    *
61    * @version $Id: 41a325f87895aabdf6eb695709fde17e364130ed $
62    * @since 3.2M1
63    */
64    @Component
65    @Named("LogbackEventGenerator")
66    @Singleton
 
67    public class LogbackEventGenerator extends AppenderBase<ILoggingEvent> implements EventListener, Initializable,
68    Disposable
69    {
70    /**
71    * The logger to log.
72    */
73    @Inject
74    private Logger logger;
75   
76    /**
77    * The component manager.
78    */
79    @Inject
80    private ComponentManager componentManager;
81   
82    /**
83    * Logback utilities.
84    */
85    private LogbackUtils utils = new LogbackUtils();
86   
 
87  492 toggle @Override
88    public String getName()
89    {
90  492 return "LogbackEventGenerator";
91    }
92   
 
93  246 toggle @Override
94    public List<Event> getEvents()
95    {
96    // We don't want to listen to any event. We just want to benefit from the Observation Manager's initialization
97    // (see the class documentation above).
98  246 return Collections.emptyList();
99    }
100   
 
101  247 toggle @Override
102    public void initialize() throws InitializationException
103    {
104    // Register appender (see the class documentation above).
105  247 ch.qos.logback.classic.Logger rootLogger = getRootLogger();
106   
107  247 if (rootLogger != null) {
108  218 setContext(rootLogger.getLoggerContext());
109  218 rootLogger.addAppender(this);
110  218 start();
111    } else {
112  29 this.logger.warn("Could not find any Logback root logger."
113    + " The logging module won't be able to catch logs.");
114    }
115    }
116   
 
117  0 toggle @Override
118    public void onEvent(Event event, Object source, Object data)
119    {
120    // Do nothing, we don't listen to any event. We just want to benefit from the Observation Manager's
121    // initialization (see the class documentation above).
122    }
123   
124    /**
125    * @return the ObservationManager implementation
126    * @throws ComponentLookupException failed to get ObservationManager implementation
127    */
 
128  3576 toggle private ObservationManager getObservationManager() throws ComponentLookupException
129    {
130  3576 return this.componentManager.getInstance(ObservationManager.class);
131    }
132   
 
133  3576 toggle @Override
134    protected void append(ILoggingEvent event)
135    {
136  3576 Throwable throwable = null;
137  3576 IThrowableProxy throwableProxy = event.getThrowableProxy();
138  3576 if (throwableProxy instanceof ThrowableProxy) {
139  80 throwable = ((ThrowableProxy) throwableProxy).getThrowable();
140    }
141   
142  3576 try {
143  3576 LogLevel logLevel = this.utils.toLogLevel(event.getLevel());
144   
145  3576 LogEvent logevent =
146    LogUtils.newLogEvent(event.getMarker(), logLevel, event.getMessage(), event.getArgumentArray(),
147    throwable, event.getTimeStamp());
148   
149  3576 getObservationManager().notify(logevent, event.getLoggerName(), null);
150    } catch (IllegalArgumentException e) {
151  0 this.logger.debug("Unsupported log level [{}]", event.getLevel());
152    } catch (ComponentLookupException e) {
153  0 this.logger.error("Can't find any implementation of [{}]", ObservationManager.class.getName(), e);
154    }
155    }
156   
157    /**
158    * @return the Logback root logger or null if Logback is not available
159    */
 
160  493 toggle protected ch.qos.logback.classic.Logger getRootLogger()
161    {
162  493 return this.utils.getRootLogger();
163    }
164   
 
165  246 toggle @Override
166    public void dispose() throws ComponentLifecycleException
167    {
168  246 stop();
169   
170    // Unregister appender
171  246 ch.qos.logback.classic.Logger rootLogger = getRootLogger();
172   
173  246 if (rootLogger != null) {
174  218 rootLogger.detachAppender(this);
175    }
176    }
177    }