Search into your coldfusion log files

Its a small search tool that could come handy if you have multiple coldfusion servers, or you have lots of things running on your server.

You need to enter the Ip address of your server. In my case all the servers are happened tp be on windows platform and installed in C:/ . Also make sure the server you are running this code should be able to access other servers.

<cfif structkeyexists(form,"submit")>
    <Cfset servervalue = form.server>
    <Cfset lfvalue = form.lf>
    <Cfset termvalue = form.term>
    <Cfset conditionvalue = form.condition>
    <Cfset nottermvalue = form.notterm>
<Cfelse>
    <Cfset servervalue = ''>
    <Cfset lfvalue = ''>
    <Cfset termvalue = ''>
    <Cfset conditionvalue = ''>
    <Cfset nottermvalue = ''>
</cfif>

<Cfform name="logdetails" id="logdetails" action="log.cfm">
    <table>
        <tr>
            <td>CF Server</td>
            <td><cfinput type="text" name="server" id="server" required="yes" message="Please enter the value" value="#servervalue#" /></td>
        </tr>
        <tr>
            <td>Log File Name</td>
            <td>
                <select name="lf" id="lf">
                    <option id="lf1" value="scheduler.log" <cfif lfvalue eq 'scheduler.log'>selected </cfif> >scheduler.log</option>
                    <option id="lf2" value="application.log" <cfif lfvalue eq 'application.log'>selected </cfif>>application.log</option>
                    <option id="lf3" value="eventgateway.log" <cfif lfvalue eq 'eventgateway.log'>selected </cfif>>eventgateway.log</option>
                    <option id="lf4" value="mail.log" <cfif lfvalue eq 'mail.log'>selected </cfif>>mail.log</option>
                    <option id="lf5" value="mailsent.log" <cfif lfvalue eq 'mailsent.log'>selected </cfif>>mailsent.log</option>
                    <option id="lf6" value="server.log" <cfif lfvalue eq 'server.log'>selected </cfif>>server.log</option>
                </select>
            </td>
        <tr>
            <td>Search Term</td>
            <td>
                <cfinput type="text" name="term" id="term"  value="#termvalue#"/>
                <select name="condition" id="condition">
                    <option value="exact" <cfif conditionvalue eq 'exact'>selected </cfif>>Exact</option>
                    <option value="or" <cfif conditionvalue eq 'or'>selected </cfif>>OR</option>
                    <option value="and" <cfif conditionvalue eq 'and'>selected </cfif>>AND</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Should not contain</td>
            <td>
                <cfinput type="text" name="notterm" id="notterm" value="#nottermvalue#"/>

            </td>
        </tr>
        <tr>
            <td><cfinput type="submit" name="submit" id="submit" /></td>
        </tr>
    </table>
</cfform>
    <Cfoutput>

        <cfif structkeyexists(form,"submit")>

            <cfif FileExists('\\#form.server#\C$\ColdFusion8\logs\#form.lf#')>
                <cfset pathToFindLog = '\\#form.server#\C$\ColdFusion8\logs\#form.lf#'>
            <cfelseif FileExists('\\#form.server#\C$\CFusionMX7\logs\#form.lf#') >
                <cfset pathToFindLog = '\\#server#\C$\CFusionMX7\logs\#form.lf#'>
            <cfelseif FileExists('\\#form.server#\C$\ColdFusion9\logs\#form.lf#')>
                <cfset pathToFindLog = '\\#form.server#\C$\ColdFusion9\logs\#form.lf#'>
            <cfelseif FileExists('\\#form.server#\ColdFusion\logs\#form.lf#')>
                <cfset pathToFindLog = '\\#form.server#\ColdFusion\logs\#form.lf#'>
            <cfelse>
                <cfset pathToFindLog = 0>
            </cfif>    

            <cfif pathToFindLog neq 0>
                <cffile action="Read" file="#pathToFindLog#" variable="logvar">
                <Table border="1">
                    <tr>
                        <td><strong>Severity</strong></td>
                        <td><strong>ThreadID</strong></td>
                        <td><strong>Date</strong></td>
                        <td><strong>Time</strong></td>
                        <td><strong>Message</strong></td>
                        <td><strong>Application</strong></td>
                    </tr>

                    <cfloop list="#logvar#" delimiters="#chr(13)##chr(10)#" index="i">
                        <Cfset i = rereplace(i,',,',', ,','ALL')>
                        <Cfset show = 1>
                        <cfset msg = ListGetAt(i,6,'","')>
                        <cfset continue = 1>

                        <cfif form.notterm neq ''>
                            <cfif findnocase('#form.notterm#',msg)>
                                <cfset show = 0>
                                <cfset continue = 0>
                            </cfif>
                        </cfif>

                        <Cfif continue and form.term neq ''>
                            <Cfset show = 0>
                            <Cfif form.condition eq 'exact'>
                                <cfif findnocase('#form.term#',msg)>
                                    <cfset show = 1>
                                    <cfset term1 = "<strong style='color:blue'>"&form.term&"</strong>" >
                                    <cfset msg = replacenocase(msg,term,term1,'ALL')>
                                </cfif>

                            <cfelseif form.condition eq 'OR'>
                                <cfloop list="#form.term#" delimiters=" " index="c">
                                    <cfif findnocase('#c#',msg)>
                                        <cfset show = 1>
                                        <cfset c1 = "<strong style='color:blue'>"&c&"</strong>" >
                                        <cfset msg = replacenocase(msg,c,c1,'ALL')>
                                    </cfif>
                                </cfloop>
                            <cfelseif form.condition eq 'AND'>

                                 <cfloop list="#form.term#" delimiters=" " index="c">

                                    <cfif findnocase('#c#',msg)>
                                        <cfset show = 1>
                                        <cfset c1 = "<strong style='color:blue'>"&c&"</strong>" >
                                        <cfset msg = replacenocase(msg,c,c1,'ALL')>
                                    <cfelse>
                                        <cfset show = 0>
                                        <cfbreak>
                                    </cfif>
                                </cfloop>
                            </cfif>
                        </Cfif>

                        <cfif show>

                            <tr>
                                <td>#ListGetAt(i,1,'","')#</td>
                                <td>#ListGetAt(i,2,'","')#</td>
                                <td>#ListGetAt(i,3,'","')#</td>
                                <td>#ListGetAt(i,4,'","')#</td>
                                <td>#msg#</td>
                                <td>#ListGetAt(i,5,'","')#</td>
                            </tr>
                        </cfif>
                    </cfloop>

            <cfelse>
                No infor for <Cfdump var="#form#">
            </cfif>

        </cfif>

    </Cfoutput>

Above code is not fully optimized. Any suggestions or questions are welcomed!

Leave a Reply

Your email address will not be published. Required fields are marked *