logviewer.jsp 17.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<%--
  -	$RCSfile$
  -	$Revision$
  -	$Date$
  -
  - Copyright (C) 2004 Jive Software. All rights reserved.
  -
  - This software is published under the terms of the GNU Public License (GPL),
  - a copy of which is included in this distribution.
--%>
Matt Tucker's avatar
Matt Tucker committed
11 12 13 14 15 16

<%@ page import="java.io.*,
                 org.jivesoftware.util.*,
                 java.text.*,
                 org.jivesoftware.util.log.Logger,
                 org.jivesoftware.messenger.auth.UnauthorizedException,
17
                 org.jivesoftware.util.JiveGlobals,
Matt Tucker's avatar
Matt Tucker committed
18
                 org.jivesoftware.messenger.user.*,
19 20
                 java.util.*,
                 org.jivesoftware.admin.AdminPageBean"
Matt Tucker's avatar
Matt Tucker committed
21
%>
22

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

Matt Tucker's avatar
Matt Tucker committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
<jsp:useBean id="admin" class="org.jivesoftware.util.WebManager"  />
<% admin.init(request, response, session, application, out ); %>

<%!
    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"};

    static final String[] REFRESHES = {"None","10","30","60","90"};

    private static HashMap parseCookie(Cookie cookie) {
        if (cookie == null || cookie.getValue() == null) {
            HashMap empty = new HashMap();
            return empty;
        }
        StringTokenizer tokenizer = new StringTokenizer(cookie.getValue(),"&");
        HashMap valueMap = new HashMap();
        while (tokenizer.hasMoreTokens()) {
            String tok = tokenizer.nextToken();
            int pos = tok.indexOf("=");
54 55 56 57
            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
58 59
            }
        }
60
        return valueMap;
Matt Tucker's avatar
Matt Tucker committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    }

    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
83
        HashMap cookie = parseCookie(CookieUtils.getCookie(request,"jiveforums.admin.logviewer"));
Matt Tucker's avatar
Matt Tucker committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97
        String[] logs = {"error", "info", "warn", "debug"};
        HashMap newCookie = new HashMap();
        HashMap updates = new HashMap();
        for (int i=0; i<logs.length; i++) {
            // Check for the value in the cookie:
            String key = logs[i] + ".size";
            long savedSize = 0L;
            if (cookie.containsKey(key)) {
                try {
                    savedSize = Long.parseLong((String)cookie.get(key));
                }
                catch (NumberFormatException nfe) {}
            }
            // Update the size in the Map:
98 99
            File logFile = new File(logDir, logs[i] + ".log");
            long currentSize = logFile.length();
Matt Tucker's avatar
Matt Tucker committed
100 101 102 103 104 105 106 107 108 109
            newCookie.put(key, ""+currentSize);
            if (currentSize != savedSize) {
                updates.put(logs[i], "true");
            }
        }
        saveCookie(response, newCookie);
        return updates;
    }
%>

110
<%
Matt Tucker's avatar
Matt Tucker committed
111
    // Get parameters
112
    String log = ParamUtils.getParameter(request, "log");
Matt Tucker's avatar
Matt Tucker committed
113 114 115 116 117
    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");
118 119 120 121
    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
122 123 124 125 126
    boolean debugEnabled = ParamUtils.getBooleanParameter(request,"debugEnabled");
    boolean wasDebugEnabled = ParamUtils.getBooleanParameter(request,"wasDebugEnabled");

    // Enable/disable debugging
    if (request.getParameter("wasDebugEnabled") != null && wasDebugEnabled != debugEnabled) {
127 128
        Log.setDebugEnabled(debugEnabled);
        response.sendRedirect("logviewer.jsp?log=debug");
Matt Tucker's avatar
Matt Tucker committed
129 130 131
        return;
    }

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    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)) {
153
            Log.markErrorLogFile(pageUser.getUsername());
154 155
        }
        else if ("warn".equals(log)) {
156
            Log.markWarnLogFile(pageUser.getUsername());
157 158
        }
        else if ("info".equals(log)) {
159
            Log.markInfoLogFile(pageUser.getUsername());
160 161
        }
        else if ("debug".equals(log)) {
162
            Log.markDebugLogFile(pageUser.getUsername());
163 164 165 166 167 168 169 170 171 172 173 174 175
        }
        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
176 177 178 179 180 181 182 183 184 185 186 187 188

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

    // Other vars
189
    File logDir = new File(Log.getLogDirectory());
190
    String filename = log + ".log";
Matt Tucker's avatar
Matt Tucker committed
191 192 193 194 195 196
    File logFile = new File(logDir, filename);

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

197
<%  // Title of this page and breadcrumbs
198
    String title = LocaleUtils.getLocalizedString("logviewer.title");
199
    pageinfo.setTitle(title);
200
    pageinfo.getBreadcrumbs().add(new AdminPageBean.Breadcrumb(LocaleUtils.getLocalizedString("global.main"), "index.jsp"));
201 202 203 204 205
    pageinfo.getBreadcrumbs().add(new AdminPageBean.Breadcrumb(title, "logviewer.jsp?log=" + log));
    pageinfo.setPageID("server-logs");
%>
<jsp:include page="top.jsp" flush="true" />
<jsp:include page="title.jsp" flush="true" />
Matt Tucker's avatar
Matt Tucker committed
206 207 208 209 210 211 212

<%  if (refreshParam != null && !"None".equals(refreshParam)) { %>

    <meta http-equiv="refresh" content="<%= refresh %>">

<%  } %>

213
<div id="logviewer">
Matt Tucker's avatar
Matt Tucker committed
214 215

<style type="text/css">
216
SELECT, INPUT {
Matt Tucker's avatar
Matt Tucker committed
217
    font-family : verdana, arial;
218
    font-size : 8pt;
Matt Tucker's avatar
Matt Tucker committed
219 220 221 222 223 224 225
}
.date {
    color : #00f;
    border-width : 0px 0px 1px 0px;
    border-style : dotted;
    border-color : #00f;
}
226 227 228 229 230 231 232 233 234 235
.buttons TD {
    padding : 3px;
}
.buttons .icon-label {
    padding-right : 1em;
}
.log-info {
    border-width : 0px 1px 1px 1px;
    border-color : #ccc;
    border-style : solid;
Matt Tucker's avatar
Matt Tucker committed
236
}
237 238
IFRAME {
    border : 1px #666 solid;
Matt Tucker's avatar
Matt Tucker committed
239 240 241
}
</style>

242
<form action="logviewer.jsp" name="logViewer" method="get">
Matt Tucker's avatar
Matt Tucker committed
243 244
<input type="hidden" name="log" value="<%= log %>">

245 246 247
<div class="jive-tabs">
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tbody>
Matt Tucker's avatar
Matt Tucker committed
248
    <tr>
249 250 251
        <td class="jive-spacer" width="1%">&nbsp;</td>
        <td class="jive-tab<%= (("error".equals(log))?"-active":"") %>" width="1%">
            <a href="logviewer.jsp?log=error"
252
            ><fmt:message key="logviewer.error" /></a>
253 254 255
            <span class="new">
            <%= ((newlogs.containsKey("error"))?"*":"") %>
            </span>
Matt Tucker's avatar
Matt Tucker committed
256
        </td>
257 258 259
        <td class="jive-spacer" width="1%">&nbsp;</td>
        <td class="jive-tab<%= (("warn".equals(log))?"-active":"") %>" width="1%">
            <a href="logviewer.jsp?log=warn"
260
            ><fmt:message key="logviewer.warn" /></a>
261 262 263
            <span class="new">
            <%= ((newlogs.containsKey("warn"))?"*":"") %>
            </span>
Matt Tucker's avatar
Matt Tucker committed
264
        </td>
265 266 267
        <td class="jive-spacer" width="1%">&nbsp;</td>
        <td class="jive-tab<%= (("info".equals(log))?"-active":"") %>" width="1%">
            <a href="logviewer.jsp?log=info"
268
            ><fmt:message key="logviewer.info" /></a>
269 270 271 272 273 274 275
            <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"
276
            ><fmt:message key="logviewer.debug" /></a>
277 278 279 280 281 282
            <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
283 284
        </td>
    </tr>
285 286
</tbody>
</table>
Matt Tucker's avatar
Matt Tucker committed
287

288 289 290 291
<%  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
292

293 294 295 296 297 298 299
<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>
300
                <td nowrap><fmt:message key="logviewer.log" /></td>
301 302
                <td nowrap><b><%= logFile.getName() %></b> (<%= byteFormatter.format(logFile.length()) %>)</td>
                <td width="96%" rowspan="3">&nbsp;</td>
303
                <td nowrap><fmt:message key="logviewer.order" /></td>
304 305 306
                <td nowrap>
                    <input type="radio" name="mode" value="asc"<%= ("asc".equals(mode)?" checked":"") %>
                     onclick="this.form.submit();" id="rb01"
307
                     ><label for="rb01"><fmt:message key="logviewer.normal" /></label>
308 309
                    <input type="radio" name="mode" value="desc"<%= ("desc".equals(mode)?" checked":"") %>
                     onclick="this.form.submit();" id="rb02"
310
                     ><label for="rb02"><fmt:message key="logviewer.reverse" /></label>
311 312 313
                </td>
            </tr>
            <tr>
314
                <td nowrap><fmt:message key="logviewer.modified" /></td>
315 316 317
                <td nowrap>
                    <span><%= dateFormatter.format(lastMod) %></span>
                </td>
318
                <td nowrap><fmt:message key="logviewer.line" /></td>
319 320 321 322 323 324 325 326 327 328
                <td nowrap>
                    <select name="lines" size="1"
                     onchange="this.form.submit();">
                        <%  for (int j=0; j<LINES.length; j++) {
                                String selected = (LINES[j].equals(numLinesParam))?" selected":"";
                        %>
                            <option value="<%= LINES[j] %>"<%= selected %>><%= LINES[j] %>

                        <%  } %>
                            <option value="All"<%= (("All".equals(numLinesParam))?" selected":"") %>
329
                             ><fmt:message key="logviewer.all" />
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
                    </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">
357
                                <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>
358 359
                            </td>
                            <td class="icon-label">
360 361
                                <a href="#" onclick="if (confirm('<fmt:message key="logviewer.clear" />')) {setLog('clearLog'); document.logViewer.submit(); return true;} else { return false; }"
                                 ><fmt:message key="logviewer.clear" /></a>
362 363
                            </td>
                            <td class="icon">
364
                                <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>
365 366 367
                            </td>
                            <td class="icon-label">
                                <a href="#" onclick="setLog('markLog'); document.logViewer.submit(); return true;"
368
                                 ><fmt:message key="logviewer.mark" /></a>
369 370 371 372 373 374
                            </td>
                        </tr>
                    </tbody>
                    </table>
                    </div>
                </td>
375
                <td nowrap><fmt:message key="logviewer.refresh" /></td>
376 377 378 379 380 381 382 383 384
                <td nowrap>
                    <select size="1" name="refresh" onchange="this.form.submit();">
                    <%  for (int j=0; j<REFRESHES.length; j++) {
                            String selected = REFRESHES[j].equals(refreshParam)?" selected":"";
                    %>
                        <option value="<%= REFRESHES[j] %>"<%= selected %>><%= REFRESHES[j] %>

                    <%  } %>
                    </select>
385
                    (<fmt:message key="logviewer.seconds" />)
386 387 388 389 390 391 392 393 394 395 396
                </td>
            </tr>

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

                <tr>
                    <td colspan="5">

                        <table cellpadding="0" cellspacing="0" border="0" width="100%">
                        <tr>
                            <td width="1%" nowrap>
397
                                <fmt:message key="logviewer.debug_log" />: &nbsp;
398 399 400 401 402
                            </td>
                            <td width="1%">
                                <input type="radio" name="debugEnabled" value="true"<%= ((debugEnabled) ? " checked" : "") %> id="de01">
                            </td>
                            <td width="1%" nowrap>
403
                                <label for="de01"><fmt:message key="logviewer.enabled" /></label> &nbsp;
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
                            </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 %>">
                                <input type="submit" name="" value="Update">
                            </td>
                            <td width="94%">&nbsp;</td>
                        </tr>
                        </table>
                    </td>
                </tr>
Matt Tucker's avatar
Matt Tucker committed
420

421
            <%  } %>
Matt Tucker's avatar
Matt Tucker committed
422

423 424 425 426 427
            </table>
            </div>
        </td>
    </tr>
</tbody>
Matt Tucker's avatar
Matt Tucker committed
428
</table>
429
</div>
Matt Tucker's avatar
Matt Tucker committed
430 431 432

<br>

433
<span class="jive-description" style="color:#666;">
434
Log dir: <%= JiveGlobals.getHomeDirectory() %><%= File.separator %>logs
Matt Tucker's avatar
Matt Tucker committed
435 436
</span>

437 438 439 440 441
<br><br>

<iframe src="log.jsp?log=<%= log %>&mode=<%= mode %>&lines=<%= ("All".equals(numLinesParam) ? "All" : String.valueOf(numLines)) %>"
    frameborder="0" height="400" width="100%" marginheight="0" marginwidth="0" scrolling="auto"></iframe>

Matt Tucker's avatar
Matt Tucker committed
442 443
</form>

444 445
</div>

Bill Lynch's avatar
Bill Lynch committed
446
<jsp:include page="bottom.jsp" flush="true" />