Useful bit of code to view your site's log files.

Discussion in 'General Web Coding' started by antic, Apr 1, 2007.

  1. antic

    antic Perch

    I've been doing some PayPal testing and needed to see when PP hit my site with the IPN callback. So I wrote this dirty little log file viewer.

    It should work on any Jodo site, but you must put it inside a subfolder, like "utility" or "cp" or something, as it uses relative paths to find the domain's log folder. e.g. www.yourdomain.com/util/showlogs.asp

    When you run the page, it shows a list of the current log files, for you to view. Click on a file to see it's contents. You can also filter the contents using the URL search box at the top. Hope this comes in handy for someone.

    Code:
    <%
    Option Explicit
    Response.Buffer = 1
    Response.Expires = -1
    
    Dim objMain
    Set objMain = New clsMain
    Response.End
    
    Class clsMain
    
      Dim sLogPath, fso, sDomain
    
      ' ----------------------------------------------------------
    
      Sub Class_Initialize
    
        Set fso = CreateObject("Scripting.FileSystemObject")
    
        sDomain = LCase(Request.ServerVariables("server_name"))
        If Left(sDomain, 4) = "www." Then sDomain = Mid(sDomain, 5)
        sLogPath = Server.MapPath("./") & "\..\..\logs\" & sDomain & "\"
    
        %>
        <html>
        <head>
        <style>
          *         { -moz-box-sizing: border-box }
          body,td   { font: normal 8pt verdana }
          input,select  { font: normal 8pt verdana }
          p         { margin: 0px }
          .thead    { height: 20px }
          .thead td { border-bottom: 1px solid #808080; border-right: 5px solid #FFFFFF;
                      padding-bottom: 2px; padding-right: 5px; padding-left: 2px; background-color: #f0f0f0 }
          tbody td  { padding: 2px 10px 2px 2px }
          a         { color: blue }
          ul,ol     { margin-top: 0px; margin-bottom: 0px; list-style-type: disc }
          li        { margin-bottom: 5px }
        </style>
        </head>
        <body>
        <%
    
        If Request.QueryString("log") = "" Then
          Call ShowLogs
        Else
          Call ShowLog
        End If
    
      End Sub
    
      Sub Class_Terminate
    
        %>
        </body>
        </html>
        <%
    
      End Sub
    
      ' ----------------------------------------------------------
    
      Sub ShowLogs
    
        Dim fl, fc, f1
    
        Set fl = fso.GetFolder(sLogPath)
        Set fc = fl.Files
    
        Response.Write "<b>Log Files Available:</b><br><br>" & vbCrLf
    
        Response.Write "<ul>" & vbCrLf
        For Each f1 in fc
          Response.Write "<li><a class=""log"" href=""showlogs.asp?log=" & f1.name & """>" & f1.name & "</a></li>" & vbCrLf
        Next
        Response.Write "</ul>" & vbCrLf
    
      End Sub
    
      ' ----------------------------------------------------------
    
      Sub ShowLog
    
        Dim ts, sLine, sSearch, bOk, iLines
    
        Set ts = fso.OpenTextFile(sLogPath & Request.QueryString("log"), 1, False)
        sSearch = LCase(Request.QueryString("search"))
    
        Response.Write "<b>Viewing Log File: " & Request.QueryString("log") & "</b>"
    
        Response.Write "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
        Response.Write "URL Search:&nbsp;"
        Response.Write "<input type=""text"" id=""urlsearch"" size=""10"" maxlength=""50"" value=""" & sSearch & """>"
        Response.Write "&nbsp;&nbsp;"
        Response.Write "<input type=""button"" value=""Go"" " & _
                       "onclick=""window.location.href='showlogs.asp?log=" & Request.QueryString("log") & _
                                 "&search=' + document.getElementById('urlsearch').value"">"
        Response.Write "&nbsp;&nbsp;&nbsp;" & vbCrLf
        Response.Write "<a href=""showlogs.asp"">Back</a><br><br>" & vbCrLf
    
        If sSearch > "" Then
          Response.Write "Searching on """ & sSearch & """ &nbsp;&nbsp; "
          Response.Write "<a href=""showlogs.asp?log=" & Request.QueryString("log") & """>clear search</a><br><br>" & vbCrLf
        End If
    
        Response.Write "<table cellspacing=""0"" cellpadding=""0"" border=""0"" width="""">" & vbCrLf
        Response.Write "<tr class=""thead"">"
        Response.Write "<td>Date</td>"
        Response.Write "<td>Time</td>"
        Response.Write "<td>Method</td>"
        Response.Write "<td>URL</td>"
        Response.Write "<td>Query</td>"
        Response.Write "<td>Agent</td>"
        Response.Write "<td>Referrer</td>"
        Response.Write "</tr>" & vbCrLf
        Response.Write "<tbody>" & vbCrLf
    
        iLines = 0
    
        While Not ts.AtEndOfStream
          sLine = Split(ts.ReadLine, " ")
          If Left(sLine(0),1) <> "#" Then
            If sSearch = "" Then
              bOk = True
            ElseIf Instr(LCase(sLine(9)), sSearch) > 0 Then
              bOk = True
            Else
              bOk = False
            End If
            If bOk Then
              iLines = iLines + 1
              Response.Write "<tr>"
              Response.Write "<td nowrap>" & sLine(0) & "</td>"
              Response.Write "<td nowrap>" & sLine(1) & "</td>"
              Response.Write "<td nowrap>" & sLine(8) & "</td>"
              Response.Write "<td nowrap>" & sLine(9) & "</td>"
              Response.Write "<td nowrap>" & sLine(10) & "</td>"
              Response.Write "<td nowrap>" & sLine(17) & "</td>"
              Response.Write "<td nowrap>" & sLine(19) & "</td>"
              Response.Write "</tr>" & vbCrLf
            End If
          End If
        Wend
        Response.Write "</tbody>" & vbCrLf
        Response.Write "</table>" & vbCrLf
        Response.Write "<br>" & vbCrLf
    
        If iLines = 0 Then
          Response.Write "No lines to display.<br>"
        End If
    
      End Sub
    
    End Class
    
    %>
    
    Note that I don't use the <thead> tag as such... there's a thead bug in Firefox so I respectfully keep away from it. :)

Share This Page

JodoHost - 26,000 hosting end-users in 100 countries
Plesk Web Hosting
VPS Hosting
H-Sphere Web Hosting
Other Services