logviewer.jsp 17.6 KB
Newer Older
1 2 3 4
<%--
  -	$Revision$
  -	$Date$
  -
5
  - Copyright (C) 2004-2008 Jive Software. All rights reserved.
6
  -
7 8 9 10 11 12 13 14 15 16 17 18
  - Licensed under the Apache License, Version 2.0 (the "License");
  - you may not use this file except in compliance with the License.
  - You may obtain a copy of the License at
  -
  -     http://www.apache.org/licenses/LICENSE-2.0
  -
  - Unless required by applicable law or agreed to in writing, software
  - distributed under the License is distributed on an "AS IS" BASIS,
  - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  - See the License for the specific language governing permissions and
  - limitations under the License.

19
--%>
Matt Tucker's avatar
Matt Tucker committed
20 21 22 23

<%@ page import="java.io.*,
                 org.jivesoftware.util.*,
                 java.text.*,
Sven Tantau's avatar
Sven Tantau committed
24
                 java.net.URLEncoder,
25
                 org.jivesoftware.util.JiveGlobals,
26
                 org.jivesoftware.openfire.user.*,
27
                 java.util.*"
Matt Tucker's avatar
Matt Tucker committed
28
%>
29

30
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
31
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %>
32 33
<jsp:useBean id="pageinfo" scope="request" class="org.jivesoftware.admin.AdminPageBean" />

Matt Tucker's avatar
Matt Tucker committed
34 35 36 37
<jsp:useBean id="admin" class="org.jivesoftware.util.WebManager"  />
<% admin.init(request, response, session, application, out ); %>

<%!
38
    static final String NONE = LocaleUtils.getLocalizedString("global.none");
39

Matt Tucker's avatar
Matt Tucker committed
40 41 42 43 44 45 46 47 48 49 50
    static final String ERROR = "error";
    static final String INFO = "info";
    static final String WARN = "warn";
    static final String DEBUG = "debug";
    static final String DEFAULT = ERROR;

    static final String ASCENDING = "asc";
    static final String DESCENDING = "desc";

    static final String[] LINES = {"50","100","250","500"};

51
    static final String[] REFRESHES = {NONE,"10","30","60","90"};
Matt Tucker's avatar
Matt Tucker committed
52 53 54

    private static HashMap parseCookie(Cookie cookie) {
        if (cookie == null || cookie.getValue() == null) {
55
            return new HashMap();
Matt Tucker's avatar
Matt Tucker committed
56 57
        }
        StringTokenizer tokenizer = new StringTokenizer(cookie.getValue(),"&");
58
        HashMap<String, String> valueMap = new HashMap<String, String>();
Matt Tucker's avatar
Matt Tucker committed
59 60 61
        while (tokenizer.hasMoreTokens()) {
            String tok = tokenizer.nextToken();
            int pos = tok.indexOf("=");
62 63 64 65
            if (pos > 0) {
                String name = tok.substring(0,pos);
                String value = tok.substring(pos+1,tok.length());
                valueMap.put(name,value);
Matt Tucker's avatar
Matt Tucker committed
66 67
            }
        }
68
        return valueMap;
Matt Tucker's avatar
Matt Tucker committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    }

    private static void saveCookie(HttpServletResponse response, HashMap cookie) {
        StringBuffer buf = new StringBuffer();
        for (Iterator iter=cookie.keySet().iterator(); iter.hasNext();) {
            String name = (String)iter.next();
            String value = (String)cookie.get(name);
            buf.append(name).append("=").append(value);
            if (iter.hasNext()) {
                buf.append("&");
            }
        }
        Cookie newCookie = new Cookie("jiveforums.admin.logviewer",buf.toString());
        newCookie.setPath("/");
        newCookie.setMaxAge(60*60*24*30); // one month
        response.addCookie(newCookie);
    }

    private static HashMap getLogUpdate(HttpServletRequest request, HttpServletResponse response,
            File logDir)
    {
        // Get the cookie associated with the log files
91
        HashMap cookie = parseCookie(CookieUtils.getCookie(request,"jiveforums.admin.logviewer"));
Matt Tucker's avatar
Matt Tucker committed
92
        String[] logs = {"error", "info", "warn", "debug"};
93 94 95
        HashMap<String,String> newCookie = new HashMap<String,String>();
        HashMap<String,String> updates = new HashMap<String,String>();
        for (String log : logs) {
Matt Tucker's avatar
Matt Tucker committed
96
            // Check for the value in the cookie:
97
            String key = log + ".size";
Matt Tucker's avatar
Matt Tucker committed
98 99 100
            long savedSize = 0L;
            if (cookie.containsKey(key)) {
                try {
101 102 103
                    savedSize = Long.parseLong((String) cookie.get(key));
                }
                catch (NumberFormatException nfe) {
Matt Tucker's avatar
Matt Tucker committed
104 105 106
                }
            }
            // Update the size in the Map:
107
            File logFile = new File(logDir, log + ".log");
108
            long currentSize = logFile.length();
109
            newCookie.put(key, "" + currentSize);
Matt Tucker's avatar
Matt Tucker committed
110
            if (currentSize != savedSize) {
111
                updates.put(log, "true");
Matt Tucker's avatar
Matt Tucker committed
112 113 114 115 116 117 118
            }
        }
        saveCookie(response, newCookie);
        return updates;
    }
%>

119
<%
Matt Tucker's avatar
Matt Tucker committed
120
    // Get parameters
121
    String log = ParamUtils.getParameter(request, "log");
Matt Tucker's avatar
Matt Tucker committed
122 123 124 125 126
    String numLinesParam = ParamUtils.getParameter(request,"lines");
    int numLines = ParamUtils.getIntParameter(request,"lines",50);
    int refresh = ParamUtils.getIntParameter(request,"refresh",10);
    String refreshParam = ParamUtils.getParameter(request,"refresh");
    String mode = ParamUtils.getParameter(request,"mode");
127 128 129 130
    boolean clearLog = ParamUtils.getBooleanParameter(request,"clearLog");
    boolean markLog = ParamUtils.getBooleanParameter(request,"markLog");
    boolean saveLog = ParamUtils.getBooleanParameter(request,"saveLog");
    boolean emailLog = ParamUtils.getBooleanParameter(request,"emailLog");
Matt Tucker's avatar
Matt Tucker committed
131 132 133 134 135
    boolean debugEnabled = ParamUtils.getBooleanParameter(request,"debugEnabled");
    boolean wasDebugEnabled = ParamUtils.getBooleanParameter(request,"wasDebugEnabled");

    // Enable/disable debugging
    if (request.getParameter("wasDebugEnabled") != null && wasDebugEnabled != debugEnabled) {
136
        Log.setDebugEnabled(debugEnabled);
137 138
        // Log the event
        admin.logEvent((debugEnabled ? "enabled" : "disabled")+" debug logging", null);
139
        response.sendRedirect("logviewer.jsp?log=debug");
Matt Tucker's avatar
Matt Tucker committed
140 141 142
        return;
    }

143 144 145 146
    // Santize variables to prevent vulnerabilities
    if (log != null) {
        log = StringUtils.escapeHTMLTags(log);
    }
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    debugEnabled = Log.isDebugEnabled();
    User pageUser = admin.getUser();

    if (clearLog && log != null) {
        if ("error".equals(log)) {
            Log.rotateErrorLogFile();
        }
        else if ("warn".equals(log)) {
            Log.rotateWarnLogFile();
        }
        else if ("info".equals(log)) {
            Log.rotateInfoLogFile();
        }
        else if ("debug".equals(log)) {
            Log.rotateDebugLogFile();
        }
        response.sendRedirect("logviewer.jsp?log=" + log);
        return;
    }
    else if (markLog && log != null) {
        if ("error".equals(log)) {
168
            Log.markErrorLogFile(pageUser.getUsername());
169 170
        }
        else if ("warn".equals(log)) {
171
            Log.markWarnLogFile(pageUser.getUsername());
172 173
        }
        else if ("info".equals(log)) {
174
            Log.markInfoLogFile(pageUser.getUsername());
175 176
        }
        else if ("debug".equals(log)) {
177
            Log.markDebugLogFile(pageUser.getUsername());
178 179 180 181 182 183 184 185 186 187 188 189 190
        }
        response.sendRedirect("logviewer.jsp?log=" + log);
        return;
    }
    else if (saveLog && log != null) {
        saveLog = false;
        response.sendRedirect(request.getContextPath() + "/servlet/JiveServlet/?log=" + log);
        return;
    }
    else if (emailLog && log != null) {
        response.sendRedirect("emaillog.jsp?log=" + log);
        return;
    }
Matt Tucker's avatar
Matt Tucker committed
191 192 193 194 195 196 197 198 199 200 201 202 203

    // Set defaults
    if (log == null) {
        log = DEFAULT;
    }
    if (mode == null) {
        mode = ASCENDING;
    }
    if (numLinesParam == null) {
        numLinesParam = "50";
    }

    // Other vars
204
    File logDir = new File(Log.getLogDirectory());
205
    String filename = log + ".log";
Matt Tucker's avatar
Matt Tucker committed
206 207 208 209 210 211
    File logFile = new File(logDir, filename);

    // Determine if any of the log files contents have been updated:
    HashMap newlogs = getLogUpdate(request, response, logDir);
%>

212 213 214 215 216 217 218
<html>
    <head>
        <title><fmt:message key="logviewer.title"/></title>
        <meta name="pageID" content="server-logs"/>
        <meta name="helpPage" content="use_the_server_logs.html"/>
    </head>
    <body>
Matt Tucker's avatar
Matt Tucker committed
219

220
<%  if (refreshParam != null && !NONE.equals(refreshParam)) { %>
Matt Tucker's avatar
Matt Tucker committed
221 222 223
    <meta http-equiv="refresh" content="<%= refresh %>">
<%  } %>

224
<div id="logviewer">
Matt Tucker's avatar
Matt Tucker committed
225 226

<style type="text/css">
227
SELECT, INPUT {
228
    font-family : verdana, arial, sans-serif;
229
    font-size : 8pt;
Matt Tucker's avatar
Matt Tucker committed
230 231 232
}
.date {
    color : #00f;
233
    border-width : 0 0 1px 0;
Matt Tucker's avatar
Matt Tucker committed
234 235 236
    border-style : dotted;
    border-color : #00f;
}
237 238 239 240 241 242 243
.buttons TD {
    padding : 3px;
}
.buttons .icon-label {
    padding-right : 1em;
}
.log-info {
244
    border-width : 0 1px 1px 1px;
245 246
    border-color : #ccc;
    border-style : solid;
Matt Tucker's avatar
Matt Tucker committed
247
}
248 249
IFRAME {
    border : 1px #666 solid;
Matt Tucker's avatar
Matt Tucker committed
250 251 252
}
</style>

253
<form action="logviewer.jsp" name="logViewer" method="get">
Sven Tantau's avatar
Sven Tantau committed
254
<input type="hidden" name="log" value="<%= StringUtils.escapeForXML(log) %>">
Matt Tucker's avatar
Matt Tucker committed
255

256
<div class="logviewer">
257 258
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tbody>
Matt Tucker's avatar
Matt Tucker committed
259
    <tr>
260 261 262
        <td class="jive-spacer" width="1%">&nbsp;</td>
        <td class="jive-tab<%= (("error".equals(log))?"-active":"") %>" width="1%">
            <a href="logviewer.jsp?log=error"
263
            ><fmt:message key="logviewer.error" /></a>
264 265 266
            <span class="new">
            <%= ((newlogs.containsKey("error"))?"*":"") %>
            </span>
Matt Tucker's avatar
Matt Tucker committed
267
        </td>
268 269 270
        <td class="jive-spacer" width="1%">&nbsp;</td>
        <td class="jive-tab<%= (("warn".equals(log))?"-active":"") %>" width="1%">
            <a href="logviewer.jsp?log=warn"
271
            ><fmt:message key="logviewer.warn" /></a>
272 273 274
            <span class="new">
            <%= ((newlogs.containsKey("warn"))?"*":"") %>
            </span>
Matt Tucker's avatar
Matt Tucker committed
275
        </td>
276 277 278
        <td class="jive-spacer" width="1%">&nbsp;</td>
        <td class="jive-tab<%= (("info".equals(log))?"-active":"") %>" width="1%">
            <a href="logviewer.jsp?log=info"
279
            ><fmt:message key="logviewer.info" /></a>
280 281 282 283 284 285 286
            <span class="new">
            <%= ((newlogs.containsKey("info"))?"*":"") %>
            </span>
        </td>
        <td class="jive-spacer" width="1%">&nbsp;</td>
        <td class="jive-tab<%= (("debug".equals(log))?"-active":"") %>" width="1%">
            <a href="logviewer.jsp?log=debug"
287
            ><fmt:message key="logviewer.debug" /></a>
288 289 290 291 292 293
            <span class="new">
            <%= ((newlogs.containsKey("debug"))?"*":"") %>
            </span>
        </td>
        <td class="jive-stretch" width="92%" align="right" nowrap>
            &nbsp;
Matt Tucker's avatar
Matt Tucker committed
294 295
        </td>
    </tr>
296 297
</tbody>
</table>
298
</div>
Matt Tucker's avatar
Matt Tucker committed
299

300 301 302 303
<%  ByteFormat byteFormatter = new ByteFormat();
    Date lastMod = new Date(logFile.lastModified());
    DateFormat dateFormatter = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
%>
Matt Tucker's avatar
Matt Tucker committed
304

305 306 307 308 309 310 311
<div class="log-info">
<table cellpadding="6" cellspacing="0" border="0" width="100%">
<tbody>
    <tr>
        <td>
            <table cellpadding="3" cellspacing="0" border="0" width="100%">
            <tr>
312
                <td nowrap><fmt:message key="logviewer.log" /></td>
Sven Tantau's avatar
Sven Tantau committed
313
                <td nowrap><b><%= StringUtils.escapeHTMLTags(logFile.getName()) %></b> (<%= byteFormatter.format(logFile.length()) %>)</td>
314
                <td width="96%" rowspan="3">&nbsp;</td>
315
                <td nowrap><fmt:message key="logviewer.order" /></td>
316 317 318
                <td nowrap>
                    <input type="radio" name="mode" value="asc"<%= ("asc".equals(mode)?" checked":"") %>
                     onclick="this.form.submit();" id="rb01"
319
                     ><label for="rb01"><fmt:message key="logviewer.normal" /></label>
320 321
                    <input type="radio" name="mode" value="desc"<%= ("desc".equals(mode)?" checked":"") %>
                     onclick="this.form.submit();" id="rb02"
322
                     ><label for="rb02"><fmt:message key="logviewer.reverse" /></label>
323 324 325
                </td>
            </tr>
            <tr>
326
                <td nowrap><fmt:message key="logviewer.modified" /></td>
327 328 329
                <td nowrap>
                    <span><%= dateFormatter.format(lastMod) %></span>
                </td>
330
                <td nowrap><fmt:message key="logviewer.line" /></td>
331 332 333
                <td nowrap>
                    <select name="lines" size="1"
                     onchange="this.form.submit();">
334 335
                        <% for (String aLINES : LINES) {
                            String selected = (aLINES.equals(numLinesParam)) ? " selected" : "";
336
                        %>
337
                        <option value="<%= aLINES %>"<%= selected %>><%= aLINES %></option>
338 339 340

                        <%  } %>
                            <option value="All"<%= (("All".equals(numLinesParam))?" selected":"") %>
341
                             ><fmt:message key="logviewer.all" /></option>
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
                    </select>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <script language="JavaScript" type="text/javascript">
                        <!--
                        function setLog(log) {
                            document.logViewer.clearLog.value = 'false';
                            document.logViewer.markLog.value = 'false';
                            document.logViewer.saveLog.value = 'false';
                            document.logViewer.emailLog.value = 'false';

                            var t = eval("document.logViewer." + log);
                            t.value = 'true';
                        }
                        // -->
                    </script>
                    <input type="hidden" name="clearLog" value="false">
                    <input type="hidden" name="markLog" value="false">
                    <input type="hidden" name="saveLog" value="false">
                    <input type="hidden" name="emailLog" value="false">
                    <div class="buttons">
                    <table cellpadding="0" cellspacing="0" border="0">
                    <tbody>
                        <tr>
                            <td class="icon">
369
                                <a href="#" onclick="if (confirm('<fmt:message key="logviewer.confirm" />')) {setLog('clearLog'); document.logViewer.submit(); return true;} else { return false; }"><img src="images/delete-16x16.gif" border="0" alt="<fmt:message key="logviewer.alt_clear" />"></a>
370 371
                            </td>
                            <td class="icon-label">
372
                                <a href="#" onclick="if (confirm('<fmt:message key="logviewer.confirm" />')) {setLog('clearLog'); document.logViewer.submit(); return true;} else { return false; }"
373
                                 ><fmt:message key="logviewer.clear" /></a>
374 375
                            </td>
                            <td class="icon">
376
                                <a href="#" onclick="setLog('markLog'); document.logViewer.submit(); return true;"><img src="images/mark-16x16.gif" border="0" alt="<fmt:message key="logviewer.alt_mark" />"></a>
377 378 379
                            </td>
                            <td class="icon-label">
                                <a href="#" onclick="setLog('markLog'); document.logViewer.submit(); return true;"
380
                                 ><fmt:message key="logviewer.mark" /></a>
381 382 383 384 385 386
                            </td>
                        </tr>
                    </tbody>
                    </table>
                    </div>
                </td>
387
                <td nowrap><fmt:message key="global.refresh" />:</td>
388 389
                <td nowrap>
                    <select size="1" name="refresh" onchange="this.form.submit();">
390 391
                    <% for (String aREFRESHES : REFRESHES) {
                        String selected = aREFRESHES.equals(refreshParam) ? " selected" : "";
392
                    %>
393
                        <option value="<%= aREFRESHES %>"<%= selected %>><%= aREFRESHES %>
394 395 396

                    <%  } %>
                    </select>
397
                    (<fmt:message key="global.seconds" />)
398 399 400 401 402 403 404 405 406 407 408
                </td>
            </tr>

            <%  if ("debug".equals(log)) { %>

                <tr>
                    <td colspan="5">

                        <table cellpadding="0" cellspacing="0" border="0" width="100%">
                        <tr>
                            <td width="1%" nowrap>
409
                                <fmt:message key="logviewer.debug_log" />: &nbsp;
410 411 412 413 414
                            </td>
                            <td width="1%">
                                <input type="radio" name="debugEnabled" value="true"<%= ((debugEnabled) ? " checked" : "") %> id="de01">
                            </td>
                            <td width="1%" nowrap>
415
                                <label for="de01"><fmt:message key="logviewer.enabled" /></label> &nbsp;
416 417 418 419 420 421 422 423 424
                            </td>
                            <td width="1%">
                                <input type="radio" name="debugEnabled" value="false"<%= ((!debugEnabled) ? " checked" : "") %> id="de02">
                            </td>
                            <td width="1%" nowrap>
                                <label for="de02">Disabled</label> &nbsp;
                            </td>
                            <td width="1%">
                                <input type="hidden" name="wasDebugEnabled" value="<%= debugEnabled %>">
425
                                <input type="submit" name="" value="<fmt:message key="global.save_changes" />">
426 427 428 429 430 431
                            </td>
                            <td width="94%">&nbsp;</td>
                        </tr>
                        </table>
                    </td>
                </tr>
Matt Tucker's avatar
Matt Tucker committed
432

433
            <%  } %>
Matt Tucker's avatar
Matt Tucker committed
434

435 436 437 438
            </table>
        </td>
    </tr>
</tbody>
Matt Tucker's avatar
Matt Tucker committed
439
</table>
440
</div>
Matt Tucker's avatar
Matt Tucker committed
441 442 443

<br>

444
<span class="jive-description" style="color:#666;">
445
<fmt:message key="logviewer.log_dir" />: <%= JiveGlobals.getHomeDirectory() %><%= File.separator %>logs
Matt Tucker's avatar
Matt Tucker committed
446 447
</span>

448 449
<br><br>

Sven Tantau's avatar
Sven Tantau committed
450
<iframe src="log.jsp?log=<%= URLEncoder.encode(log) %>&mode=<%= URLEncoder.encode(mode) %>&lines=<%= ("All".equals(numLinesParam) ? "All" : String.valueOf(numLines)) %>"
451 452
    frameborder="0" height="400" width="100%" marginheight="0" marginwidth="0" scrolling="auto"></iframe>

Matt Tucker's avatar
Matt Tucker committed
453 454
</form>

455 456
</div>

457
    </body>
Sven Tantau's avatar
Sven Tantau committed
458
</html>