1. Project Clover database Sat Feb 2 2019 06:45:20 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 120 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  228 toggle @Override
81    public void initialize() throws InitializationException
82    {
83    // Register appender
84  228 ch.qos.logback.classic.Logger rootLogger = getRootLogger();
85   
86  228 if (rootLogger != null) {
87  189 Iterator<Appender<ILoggingEvent>> iterator = rootLogger.iteratorForAppenders();
88   
89  496 while (iterator.hasNext()) {
90  307 Appender<ILoggingEvent> appender = iterator.next();
91   
92  307 if (!(appender instanceof LogbackEventGenerator)) {
93  217 appender.addFilter(this.forbiddenThreads);
94    }
95    }
96    } else {
97  39 this.logger.warn("Could not find any Logback root logger."
98    + " All logging module advanced features will be disabled.");
99    }
100    }
101   
 
102  609 toggle @Override
103    public void pushLogListener(EventListener listener)
104    {
105  609 Stack<EventListener> listenerStack = this.listeners.get();
106   
107  609 if (listenerStack == null) {
108  409 listenerStack = new Stack<EventListener>();
109  409 this.listeners.set(listenerStack);
110    }
111   
112  609 if (!listenerStack.isEmpty()) {
113  2 this.observation.removeListener(listenerStack.peek().getName());
114    }
115   
116  608 if (listener != null) {
117  446 this.observation.addListener(new WrappedThreadEventListener(listener));
118    }
119  609 if (listenerStack.isEmpty()) {
120  607 grabLog(Thread.currentThread());
121    }
122  609 listenerStack.push(listener);
123    }
124   
 
125  607 toggle @Override
126    public EventListener popLogListener()
127    {
128  607 Stack<EventListener> listenerStack = this.listeners.get();
129   
130  607 EventListener listener;
131  607 if (listenerStack != null && !listenerStack.isEmpty()) {
132  607 listener = listenerStack.pop();
133  606 if (listener != null) {
134  444 this.observation.removeListener(listener.getName());
135    }
136  607 if (listenerStack.isEmpty()) {
137  605 ungrabLog(Thread.currentThread());
138    } else {
139  2 EventListener topListener = listenerStack.peek();
140  2 if (topListener != null) {
141  2 this.observation.addListener(new WrappedThreadEventListener(topListener));
142    }
143    }
144    } else {
145  0 listener = null;
146    }
147   
148  607 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  607 toggle private void grabLog(Thread thread)
157    {
158  607 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  605 toggle private void ungrabLog(Thread thread)
167    {
168  605 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  18149 toggle @Override
183    public LogLevel getLoggerLevel(String loggerName)
184    {
185  18149 LoggerContext loggerContext = getLoggerContext();
186   
187  18149 if (loggerContext != null) {
188  18148 ch.qos.logback.classic.Logger askedLogger = loggerContext.exists(loggerName);
189   
190  18148 if (askedLogger != null) {
191  18148 return this.utils.toLogLevel(askedLogger.getLevel());
192    }
193    }
194   
195  1 return null;
196    }
197   
 
198  13 toggle @Override
199    public Collection<Logger> getLoggers()
200    {
201  13 return (Collection) this.utils.getLoggerContext().getLoggerList();
202    }
203   
204    /**
205    * @return the Logback root logger or null if Logback is not available
206    */
 
207  228 toggle protected ch.qos.logback.classic.Logger getRootLogger()
208    {
209  228 return this.utils.getRootLogger();
210    }
211   
212    /**
213    * @return the Logback Context or null if Logback is not available
214    */
 
215  18149 toggle protected LoggerContext getLoggerContext()
216    {
217  18149 return this.utils.getLoggerContext();
218    }
219    }