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

File DefaultLoggerManager.java

 

Coverage histogram

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

Code metrics

28
47
10
1
219
131
25
0.53
4.7
10
2.5

Classes

Class Line # Actions
DefaultLoggerManager 51 47 0% 25 5
0.941176594.1%
 

Contributing tests

This file is covered by 113 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.Collection;
23    import java.util.Iterator;
24    import java.util.Stack;
25   
26    import javax.inject.Inject;
27    import javax.inject.Singleton;
28   
29    import org.slf4j.Logger;
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.component.phase.Initializable;
32    import org.xwiki.component.phase.InitializationException;
33    import org.xwiki.logging.LogLevel;
34    import org.xwiki.logging.LoggerManager;
35    import org.xwiki.observation.EventListener;
36    import org.xwiki.observation.ObservationManager;
37    import org.xwiki.observation.WrappedThreadEventListener;
38   
39    import ch.qos.logback.classic.LoggerContext;
40    import ch.qos.logback.classic.spi.ILoggingEvent;
41    import ch.qos.logback.core.Appender;
42   
43    /**
44    * Default implementation of {@link LoggerManager}.
45    *
46    * @version $Id: 660732e57c9e0ff04fb6bbc2879c019ec48cba3e $
47    * @since 3.2M3
48    */
49    @Component
50    @Singleton
 
51    public class DefaultLoggerManager implements LoggerManager, Initializable
52    {
53    /**
54    * Used to register/unregister {@link org.xwiki.logging.event.LogEvent} listeners.
55    */
56    @Inject
57    private ObservationManager observation;
58   
59    /**
60    * The logger.
61    */
62    @Inject
63    private Logger logger;
64   
65    /**
66    * The stack of listeners for the current thread.
67    */
68    private ThreadLocal<Stack<EventListener>> listeners = new ThreadLocal<Stack<EventListener>>();
69   
70    /**
71    * Logback utilities.
72    */
73    private LogbackUtils utils = new LogbackUtils();
74   
75    /**
76    * Filter forbidden threads for {@link Appender}s.
77    */
78    private ForbiddenThreadsFilter forbiddenThreads = new ForbiddenThreadsFilter();
79   
 
80  201 toggle @Override
81    public void initialize() throws InitializationException
82    {
83    // Register appender
84  201 ch.qos.logback.classic.Logger rootLogger = getRootLogger();
85   
86  201 if (rootLogger != null) {
87  172 Iterator<Appender<ILoggingEvent>> iterator = rootLogger.iteratorForAppenders();
88   
89  451 while (iterator.hasNext()) {
90  279 Appender<ILoggingEvent> appender = iterator.next();
91   
92  279 if (!(appender instanceof LogbackEventGenerator)) {
93  200 appender.addFilter(this.forbiddenThreads);
94    }
95    }
96    } else {
97  29 this.logger.warn("Could not find any Logback root logger."
98    + " All logging module advanced features will be disabled.");
99    }
100    }
101   
 
102  454 toggle @Override
103    public void pushLogListener(EventListener listener)
104    {
105  454 Stack<EventListener> listenerStack = this.listeners.get();
106   
107  454 if (listenerStack == null) {
108  264 listenerStack = new Stack<EventListener>();
109  264 this.listeners.set(listenerStack);
110    }
111   
112  453 if (!listenerStack.isEmpty()) {
113  4 this.observation.removeListener(listenerStack.peek().getName());
114    }
115   
116  453 if (listener != null) {
117  292 this.observation.addListener(new WrappedThreadEventListener(listener));
118    }
119  453 if (listenerStack.isEmpty()) {
120  450 grabLog(Thread.currentThread());
121    }
122  454 listenerStack.push(listener);
123    }
124   
 
125  453 toggle @Override
126    public EventListener popLogListener()
127    {
128  453 Stack<EventListener> listenerStack = this.listeners.get();
129   
130  452 EventListener listener;
131  453 if (listenerStack != null && !listenerStack.isEmpty()) {
132  453 listener = listenerStack.pop();
133  453 if (listener != null) {
134  291 this.observation.removeListener(listener.getName());
135    }
136  452 if (listenerStack.isEmpty()) {
137  449 ungrabLog(Thread.currentThread());
138    } else {
139  4 EventListener topListener = listenerStack.peek();
140  4 if (topListener != null) {
141  4 this.observation.addListener(new WrappedThreadEventListener(topListener));
142    }
143    }
144    } else {
145  0 listener = null;
146    }
147   
148  453 return listener;
149    }
150   
151    /**
152    * Isolate all appender from provided thread except the event generator one.
153    *
154    * @param thread the thread to remove from the log appender
155    */
 
156  449 toggle private void grabLog(Thread thread)
157    {
158  449 this.forbiddenThreads.addThread(thread);
159    }
160   
161    /**
162    * Restore all appender for the current provided thread.
163    *
164    * @param thread the thread to restore in the log appender
165    */
 
166  448 toggle private void ungrabLog(Thread thread)
167    {
168  448 this.forbiddenThreads.removeThread(thread);
169    }
170   
 
171  2 toggle @Override
172    public void setLoggerLevel(String loggerName, LogLevel logLevel)
173    {
174  2 LoggerContext loggerContext = this.utils.getLoggerContext();
175   
176  2 if (loggerContext != null) {
177  2 ch.qos.logback.classic.Logger askedLogger = loggerContext.getLogger(loggerName);
178  2 askedLogger.setLevel(this.utils.toLevel(logLevel));
179    }
180    }
181   
 
182  2 toggle @Override
183    public LogLevel getLoggerLevel(String loggerName)
184    {
185  2 LoggerContext loggerContext = getLoggerContext();
186   
187  2 if (loggerContext != null) {
188  1 ch.qos.logback.classic.Logger askedLogger = loggerContext.exists(loggerName);
189   
190  1 if (askedLogger != null) {
191  1 return this.utils.toLogLevel(askedLogger.getLevel());
192    }
193    }
194   
195  1 return null;
196    }
197   
 
198  1 toggle @Override
199    public Collection<Logger> getLoggers()
200    {
201  1 return (Collection) this.utils.getLoggerContext().getLoggerList();
202    }
203   
204    /**
205    * @return the Logback root logger or null if Logback is not available
206    */
 
207  201 toggle protected ch.qos.logback.classic.Logger getRootLogger()
208    {
209  201 return this.utils.getRootLogger();
210    }
211   
212    /**
213    * @return the Logback Context or null if Logback is not available
214    */
 
215  2 toggle protected LoggerContext getLoggerContext()
216    {
217  2 return this.utils.getLoggerContext();
218    }
219    }