File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\logging\LogRecorderManager.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.logging;
25
26 import hudson.FeedAdapter;
27 import hudson.Functions;
28 import hudson.init.Initializer;
29 import static hudson.init.InitMilestone.PLUGINS_PREPARED;
30 import hudson.model.AbstractModelObject;
31 import jenkins.model.Jenkins;
32 import hudson.model.RSS;
33 import hudson.util.CopyOnWriteMap;
34 import jenkins.model.JenkinsLocationConfiguration;
35 import org.apache.commons.io.filefilter.WildcardFileFilter;
36 import org.kohsuke.stapler.QueryParameter;
37 import org.kohsuke.stapler.StaplerRequest;
38 import org.kohsuke.stapler.StaplerResponse;
39 import org.kohsuke.stapler.HttpResponse;
40 import org.kohsuke.stapler.HttpRedirect;
41
42 import javax.servlet.ServletException;
43 import java.io.File;
44 import java.io.FileFilter;
45 import java.io.IOException;
46 import java.util.ArrayList;
47 import java.util.Calendar;
48 import java.util.GregorianCalendar;
49 import java.util.List;
50 import java.util.Locale;
51 import java.util.Map;
52 import java.util.logging.Level;
53 import java.util.logging.LogRecord;
54 import java.util.logging.Logger;
55
56 /**
57 * Owner of {@link LogRecorder}s, bound to "/log".
58 *
59 * @author Kohsuke Kawaguchi
60 */
61 public class LogRecorderManager extends AbstractModelObject {
62 /**
63 * {@link LogRecorder}s.
64 */
65 public transient final Map<String,LogRecorder> logRecorders = new CopyOnWriteMap.Tree<String,LogRecorder>();
66
67 public String getDisplayName() {
68 return Messages.LogRecorderManager_DisplayName();
69 }
70
71 public String getSearchUrl() {
72 return "/log";
73 }
74
75 public LogRecorder getDynamic(String token) {
76 return getLogRecorder(token);
77 }
78
79 public LogRecorder getLogRecorder(String token) {
80 return logRecorders.get(token);
81 }
82
83 /**
84 * Loads the configuration from disk.
85 */
86 public void load() throws IOException {
87 logRecorders.clear();
88 File dir = new File(Jenkins.getInstance().getRootDir(), "log");
89 File[] files = dir.listFiles((FileFilter)new WildcardFileFilter("*.xml"));
90 if(files==null) return;
91 for (File child : files) {
92 String name = child.getName();
93 name = name.substring(0,name.length()-4); // cut off ".xml"
94 LogRecorder lr = new LogRecorder(name);
95 lr.load();
96 logRecorders.put(name,lr);
97 }
98 }
99
100 /**
101 * Creates a new log recorder.
102 */
103 public HttpResponse doNewLogRecorder(@QueryParameter String name) {
104 Jenkins.checkGoodName(name);
105
106 logRecorders.put(name,new LogRecorder(name));
107
108 // redirect to the config screen
109 return new HttpRedirect(name+"/configure");
110 }
111
112 /**
113 * Configure the logging level.
114 */
115 @edu.umd.cs.findbugs.annotations.SuppressWarnings("LG_LOST_LOGGER_DUE_TO_WEAK_REFERENCE")
116 public HttpResponse doConfigLogger(@QueryParameter String name, @QueryParameter String level) {
117 Jenkins.getInstance().checkPermission(Jenkins.ADMINISTER);
118 Level lv;
119 if(level.equals("inherit"))
120 lv = null;
121 else
122 lv = Level.parse(level.toUpperCase(Locale.ENGLISH));
123 Logger.getLogger(name).setLevel(lv);
124 return new HttpRedirect("levels");
125 }
126
127 /**
128 * RSS feed for log entries.
129 */
130 public void doRss( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException {
131 doRss(req, rsp, Jenkins.logRecords);
132 }
133
134 /**
135 * Renders the given log recorders as RSS.
136 */
137 /*package*/ static void doRss(StaplerRequest req, StaplerResponse rsp, List<LogRecord> logs) throws IOException, ServletException {
138 // filter log records based on the log level
139 String level = req.getParameter("level");
140 if(level!=null) {
141 Level threshold = Level.parse(level);
142 List<LogRecord> filtered = new ArrayList<LogRecord>();
143 for (LogRecord r : logs) {
144 if(r.getLevel().intValue() >= threshold.intValue())
145 filtered.add(r);
146 }
147 logs = filtered;
148 }
149
150 RSS.forwardToRss("Hudson log","", logs, new FeedAdapter<LogRecord>() {
151 public String getEntryTitle(LogRecord entry) {
152 return entry.getMessage();
153 }
154
155 public String getEntryUrl(LogRecord entry) {
156 return "log"; // TODO: one URL for one log entry?
157 }
158
159 public String getEntryID(LogRecord entry) {
160 return String.valueOf(entry.getSequenceNumber());
161 }
162
163 public String getEntryDescription(LogRecord entry) {
164 return Functions.printLogRecord(entry);
165 }
166
167 public Calendar getEntryTimestamp(LogRecord entry) {
168 GregorianCalendar cal = new GregorianCalendar();
169 cal.setTimeInMillis(entry.getMillis());
170 return cal;
171 }
172
173 public String getEntryAuthor(LogRecord entry) {
174 return JenkinsLocationConfiguration.get().getAdminAddress();
175 }
176 },req,rsp);
177 }
178
179 @Initializer(before=PLUGINS_PREPARED)
180 public static void init(Jenkins h) throws IOException {
181 h.getLog().load();
182 }
183 }
184