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

File LoggingScriptService.java

 

Coverage histogram

../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

6
19
6
1
165
74
9
0.47
3.17
6
1.5

Classes

Class Line # Actions
LoggingScriptService 53 19 0% 9 31
0.00%
 

Contributing tests

No tests hitting this source file were found.

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.script;
21   
22    import java.util.Collection;
23    import java.util.HashMap;
24    import java.util.Map;
25   
26    import javax.inject.Inject;
27    import javax.inject.Named;
28    import javax.inject.Singleton;
29   
30    import org.slf4j.Logger;
31    import org.slf4j.LoggerFactory;
32    import org.xwiki.component.annotation.Component;
33    import org.xwiki.localization.ContextualLocalizationManager;
34    import org.xwiki.localization.Translation;
35    import org.xwiki.logging.LogLevel;
36    import org.xwiki.logging.LogTree;
37    import org.xwiki.logging.LogUtils;
38    import org.xwiki.logging.LoggerManager;
39    import org.xwiki.logging.event.LogEvent;
40    import org.xwiki.script.service.ScriptService;
41    import org.xwiki.security.authorization.ContextualAuthorizationManager;
42    import org.xwiki.security.authorization.Right;
43   
44    /**
45    * Provide logging related script oriented APIs.
46    *
47    * @since 4.2M3
48    * @version $Id: 04c0236e7ed0e9dcefb1842ee462b904c934958b $
49    */
50    @Component
51    @Named("logging")
52    @Singleton
 
53    public class LoggingScriptService implements ScriptService
54    {
55    /**
56    * Used to manipulate loggers.
57    */
58    @Inject
59    private LoggerManager loggerManager;
60   
61    /**
62    * Used to check rights.
63    */
64    @Inject
65    private ContextualAuthorizationManager authorization;
66   
67    @Inject
68    private ContextualLocalizationManager localization;
69   
70    /**
71    * Return a logger named according to the name parameter.
72    *
73    * @param name The name of the logger.
74    * @return logger
75    * @since 6.1RC1
76    */
 
77  0 toggle public Logger getLogger(String name)
78    {
79  0 return LoggerFactory.getLogger(name);
80    }
81   
82    // Get/Set log levels
83   
84    /**
85    * @return all the loggers (usually packages) with corresponding levels.
86    */
 
87  0 toggle public Map<String, LogLevel> getLevels()
88    {
89  0 Collection<Logger> registeredLoggers = this.loggerManager.getLoggers();
90   
91  0 Map<String, LogLevel> levels = new HashMap<String, LogLevel>(registeredLoggers.size());
92   
93  0 for (Logger registeredLogger : registeredLoggers) {
94  0 levels.put(registeredLogger.getName(), getLevel(registeredLogger.getName()));
95    }
96   
97  0 return levels;
98    }
99   
100    /**
101    * @param loggerName the logger name (usually packages)
102    * @return the level associated to the logger
103    */
 
104  0 toggle public LogLevel getLevel(String loggerName)
105    {
106  0 return this.loggerManager.getLoggerLevel(loggerName);
107    }
108   
109    /**
110    * @param logger the logger name (usually package)
111    * @param level the level associated to the logger
112    */
 
113  0 toggle public void setLevel(String logger, LogLevel level)
114    {
115    // Not allow anyone to set log level or it could be a window to produce a real mess even if not exactly a
116    // security issue
117  0 if (!this.authorization.hasAccess(Right.PROGRAM)) {
118  0 return;
119    }
120   
121  0 this.loggerManager.setLoggerLevel(logger, level);
122    }
123   
124    /**
125    * Create a tree representation of a series of logs.
126    *
127    * @param logs the logs
128    * @return the logs as a {@link LogTree}
129    * @since 5.4RC1
130    */
 
131  0 toggle public LogTree toLogTree(Iterable<LogEvent> logs)
132    {
133  0 LogTree logTree = new LogTree();
134   
135  0 for (LogEvent logEvent : logs) {
136  0 logTree.log(logEvent);
137    }
138   
139  0 return logTree;
140    }
141   
142    /**
143    * Translate the passed {@link LogEvent} based on the translation message corresponding to the translation key
144    * stored in the {@link LogEvent}.
145    * <p>
146    * The translation message pattern use the same syntax than standard message pattern except that it's optionally
147    * possible to provide a custom index as in <code>Some {1} translation {0} message</code> in order to modify the
148    * order of the argument which can be required depending on the language.
149    *
150    * @param logEvent the {@link LogEvent} to translate
151    * @return the translated version of the passed {@link LogEvent}
152    */
 
153  0 toggle public LogEvent translate(LogEvent logEvent)
154    {
155  0 if (logEvent.getTranslationKey() != null) {
156  0 Translation translation = this.localization.getTranslation(logEvent.getTranslationKey());
157   
158  0 if (translation != null) {
159  0 return LogUtils.translate(logEvent, (String) translation.getRawSource());
160    }
161    }
162   
163  0 return logEvent;
164    }
165    }