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

File LogEvent.java

 

Coverage histogram

../../../../img/srcFileCovDistChart6.png
69% of files have more coverage

Code metrics

6
57
14
1
240
127
27
0.47
4.07
14
1.93

Classes

Class Line # Actions
LogEvent 44 57 0% 27 36
0.5324675453.2%
 

Contributing tests

This file is covered by 190 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.event;
21   
22    import org.apache.commons.lang3.ArrayUtils;
23    import org.apache.commons.lang3.builder.HashCodeBuilder;
24    import org.slf4j.Logger;
25    import org.slf4j.Marker;
26    import org.slf4j.MarkerFactory;
27    import org.xwiki.logging.LogLevel;
28    import org.xwiki.logging.Message;
29    import org.xwiki.observation.event.Event;
30   
31    /**
32    * Sent when logger is called.
33    * <p>
34    * The event also send the following parameters:
35    * </p>
36    * <ul>
37    * <li>source: the name of the associated logger</li>
38    * <li>data: null</li>
39    * </ul>
40    *
41    * @version $Id: 0fce662ebba274a1b6a2b9a0a48ff7f8725a7a82 $
42    * @since 3.2M1
43    */
 
44    public class LogEvent extends Message implements Event
45    {
46    /**
47    * The marker to use to indicate that we start a group of logs.
48    */
49    public static final Marker MARKER_BEGIN = MarkerFactory.getMarker("xwiki.begin");
50   
51    /**
52    * The marker to use to indicate that we stop a group of logs.
53    */
54    public static final Marker MARKER_END = MarkerFactory.getMarker("xwiki.end");
55   
56    /**
57    * @see #getLevel()
58    */
59    private LogLevel level;
60   
61    /**
62    * The number of milliseconds elapsed from 1/1/1970 until logging event was created.
63    */
64    private long timeStamp;
65   
66    /**
67    * Matches any {@link LogEvent}.
68    */
 
69  139 toggle public LogEvent()
70    {
71   
72    }
73   
74    /**
75    * @param logEvent the log event to copy
76    * @since 5.4M1
77    */
 
78  9 toggle public LogEvent(LogEvent logEvent)
79    {
80  9 this(logEvent.getMarker(), logEvent.getLevel(), logEvent.getMessage(), logEvent.getArgumentArray(),
81    logEvent.getThrowable(), logEvent.getTimeStamp());
82    }
83   
84    /**
85    * @param level the log level
86    * @param message the log message
87    * @param argumentArray the event arguments to insert in the message
88    * @param throwable the throwable associated to the event
89    */
 
90  1 toggle public LogEvent(LogLevel level, String message, Object[] argumentArray, Throwable throwable)
91    {
92  1 this(null, level, message, argumentArray, throwable);
93    }
94   
95    /**
96    * @param marker the log marker
97    * @param level the log level
98    * @param message the log message
99    * @param argumentArray the event arguments to insert in the message
100    * @param throwable the throwable associated to the event
101    * @since 4.3M
102    */
 
103  8 toggle public LogEvent(Marker marker, LogLevel level, String message, Object[] argumentArray, Throwable throwable)
104    {
105  8 this(marker, level, message, argumentArray, throwable, System.currentTimeMillis());
106    }
107   
108    /**
109    * @param marker the log marker
110    * @param level the log level
111    * @param message the log message
112    * @param argumentArray the event arguments to insert in the message
113    * @param throwable the throwable associated to the event
114    * @param timeStamp the number of milliseconds elapsed from 1/1/1970 until logging event was created.
115    * @since 6.4M1
116    */
 
117  3908 toggle public LogEvent(Marker marker, LogLevel level, String message, Object[] argumentArray, Throwable throwable,
118    long timeStamp)
119    {
120  3908 super(marker, message, argumentArray, throwable);
121   
122  3908 this.level = level;
123  3908 this.timeStamp = timeStamp;
124    }
125   
126    /**
127    * Copy the stored log into a passed {@link Logger}.
128    *
129    * @param targetLogger the logger where to copy the stored log
130    * @since 5.3M1
131    */
 
132  14 toggle public void log(Logger targetLogger)
133    {
134  14 Object[] argumentArray = getArgumentArray();
135  14 if (getArgumentArray() == null) {
136  1 log(targetLogger, getLevel(), getMarker(), getMessage(), getThrowable());
137    } else {
138  13 if (getThrowable() != null) {
139  0 argumentArray = ArrayUtils.add(getArgumentArray(), getThrowable());
140    }
141   
142  13 log(targetLogger, getLevel(), getMarker(), getMessage(), argumentArray);
143    }
144    }
145   
 
146  13 toggle private static void log(Logger targetLogger, LogLevel level, Marker marker, String message, Object[] argumentArray)
147    {
148  13 switch (level) {
149  0 case TRACE:
150  0 targetLogger.trace(marker, message, argumentArray);
151  0 break;
152  0 case DEBUG:
153  0 targetLogger.debug(marker, message, argumentArray);
154  0 break;
155  0 case INFO:
156  0 targetLogger.info(marker, message, argumentArray);
157  0 break;
158  0 case WARN:
159  0 targetLogger.warn(marker, message, argumentArray);
160  0 break;
161  13 case ERROR:
162  13 targetLogger.error(marker, message, argumentArray);
163  13 break;
164  0 default:
165  0 break;
166    }
167    }
168   
 
169  1 toggle private static void log(Logger targetLogger, LogLevel level, Marker marker, String message, Throwable throwable)
170    {
171  1 switch (level) {
172  0 case TRACE:
173  0 targetLogger.trace(marker, message, throwable);
174  0 break;
175  0 case DEBUG:
176  0 targetLogger.debug(marker, message, throwable);
177  0 break;
178  0 case INFO:
179  0 targetLogger.info(marker, message, throwable);
180  0 break;
181  0 case WARN:
182  0 targetLogger.warn(marker, message, throwable);
183  0 break;
184  1 case ERROR:
185  1 targetLogger.error(marker, message, throwable);
186  1 break;
187  0 default:
188  0 break;
189    }
190    }
191   
192    /**
193    * @return the log level
194    */
 
195  7186 toggle public LogLevel getLevel()
196    {
197  7186 return this.level;
198    }
199   
200    /**
201    * @return the number of milliseconds elapsed from 1/1/1970 until logging event was created
202    */
 
203  384 toggle public long getTimeStamp()
204    {
205  384 return this.timeStamp;
206    }
207   
208    // Event
209   
 
210  4097 toggle @Override
211    public boolean matches(Object otherEvent)
212    {
213  4097 return true;
214    }
215   
216    // Object
217   
 
218  0 toggle @Override
219    public String toString()
220    {
221  0 return getLevel().toString() + ':' + super.toString();
222    }
223   
 
224  0 toggle @Override
225    public int hashCode()
226    {
227  0 return new HashCodeBuilder(7, 11).append(getLevel()).appendSuper(super.hashCode()).toHashCode();
228    }
229   
 
230  2 toggle @Override
231    public boolean equals(Object object)
232    {
233  2 if (!super.equals(object)) {
234  0 return false;
235    }
236   
237  2 LogEvent rhs = (LogEvent) object;
238  2 return getLevel() == rhs.getLevel();
239    }
240    }