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

File LogUtils.java

 

Coverage histogram

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

Code metrics

14
22
4
1
123
55
11
0.5
5.5
4
2.75

Classes

Class Line # Actions
LogUtils 34 22 0% 11 3
0.92592.5%
 

Contributing tests

This file is covered by 179 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;
21   
22    import org.slf4j.Marker;
23    import org.xwiki.logging.event.BeginLogEvent;
24    import org.xwiki.logging.event.EndLogEvent;
25    import org.xwiki.logging.event.LogEvent;
26    import org.xwiki.logging.internal.helpers.MessageParser;
27    import org.xwiki.logging.internal.helpers.MessageParser.MessageElement;
28    import org.xwiki.logging.internal.helpers.MessageParser.MessageIndex;
29   
30    /**
31    * @version $Id: 0c1c406b1e6483c68d3df13ff6b5f310a054ff84 $
32    * @since 5.4RC1
33    */
 
34    public final class LogUtils
35    {
 
36  0 toggle private LogUtils()
37    {
38    // Utility class
39    }
40   
41    /**
42    * Create and return a new {@link LogEvent} instance based on the passed parameters.
43    *
44    * @param marker the log marker
45    * @param level the log level
46    * @param message the log message
47    * @param argumentArray the event arguments to insert in the message
48    * @param throwable the throwable associated to the event
49    * @return the {@link LogEvent}
50    */
 
51  184 toggle public static LogEvent newLogEvent(Marker marker, LogLevel level, String message, Object[] argumentArray,
52    Throwable throwable)
53    {
54  184 return newLogEvent(marker, level, message, argumentArray, throwable, System.currentTimeMillis());
55    }
56   
57    /**
58    * Create and return a new {@link LogEvent} instance based on the passed parameters.
59    *
60    * @param marker the log marker
61    * @param level the log level
62    * @param message the log message
63    * @param argumentArray the event arguments to insert in the message
64    * @param throwable the throwable associated to the event
65    * @param timeStamp the number of milliseconds elapsed from 1/1/1970 until logging event was created.
66    * @return the {@link LogEvent}
67    * @since 6.4M1
68    */
 
69  3760 toggle public static LogEvent newLogEvent(Marker marker, LogLevel level, String message, Object[] argumentArray,
70    Throwable throwable, long timeStamp)
71    {
72  3760 if (marker != null) {
73  3026 if (marker.contains(LogEvent.MARKER_BEGIN)) {
74  1115 return new BeginLogEvent(marker, level, message, argumentArray, throwable, timeStamp);
75  1911 } else if (marker.contains(LogEvent.MARKER_END)) {
76  1115 return new EndLogEvent(marker, level, message, argumentArray, throwable, timeStamp);
77    }
78    }
79   
80  1530 return new LogEvent(marker, level, message, argumentArray, throwable, timeStamp);
81    }
82   
83    /**
84    * Translate the passed {@link LogEvent} based on the passed translation message pattern.
85    * <p>
86    * The translation message pattern use the same syntax than standard message pattern except that it's optionally
87    * possible to provide a custom index as in <code>Some {1} translation {0} message</code> in order to modify the
88    * order of the argument which can be required depending on the language.
89    *
90    * @param logEvent the {@link LogEvent} to translate
91    * @param translatedMessage the translated version of the {@link LogEvent} message
92    * @return the translated version of the passed {@link LogEvent}
93    */
 
94  5 toggle public static LogEvent translate(LogEvent logEvent, String translatedMessage)
95    {
96  5 if (translatedMessage != null) {
97  5 MessageParser parser = new MessageParser(translatedMessage, true);
98   
99  5 Object[] defaultArguments = logEvent.getArgumentArray();
100  5 Object[] arguments = new Object[defaultArguments.length];
101  5 StringBuilder message = new StringBuilder();
102   
103  5 int index = 0;
104  21 for (MessageElement element = parser.next(); element != null; element = parser.next()) {
105  16 if (element instanceof MessageIndex) {
106  8 message.append(MessageParser.ARGUMENT_STR);
107  8 arguments[index++] = defaultArguments[((MessageIndex) element).getIndex()];
108    } else {
109  8 message.append(element.getString());
110    }
111    }
112   
113  7 for (; index < arguments.length; ++index) {
114  2 arguments[index] = defaultArguments[index];
115    }
116   
117  5 return new LogEvent(logEvent.getMarker(), logEvent.getLevel(), message.toString(), arguments,
118    logEvent.getThrowable(), logEvent.getTimeStamp());
119    }
120   
121  0 return logEvent;
122    }
123    }