Skip to content Skip to sidebar Skip to footer

Parsing Log Files In A Folder In Coldfusion

The problem is there is a folder ./log/ containing the files like: jan2010.xml, feb2010.xml, mar2010.xml, jan2009.xml, feb2009.xml, mar2009.xml ... each xml file would like:

Solution 1:

The short answer is that, if your XML is correctly formatted, you can use the XMLParse() function to get the XML into a CF data object.

Sergii pointed out that XMLParse cna take a path, so you can simply read the file directly into it and assign the results to a variable.

The data should look something like an array of structures. Use CFDUMP on your CF data object to view it and help you figure it out.

Solution 2:

I would strongly urge you to check out Microsoft's "Log Parser" if you're on Windows. It provides essentially a SQL-like query interface to all manner of log files. There's an app called "Log Parser Lizard" which provides a GUI for testing out "queries", and then you can cfexecute logparser with the queries you come up with.

Not sure if there's an equivalent for linux.

If you're on Windows and interested in hearing more, let me know. I had it hooked into an ANT task that downloaded log files every night, parsed them, generated reports, etc. It worked very well.

Solution 3:

Concrete example:

<CFSETyear = 2011 /><CFDIRECTORYdirectory="#expandpath("log")#" action="list"sort="name"name="logfiles"filter="*#year#.xml" /><CFOUTPUTquery="logfiles"><CFSETsinglelogfile = xmlparse(directory & "/" & name) /><CFSETrecords = XmlSearch(singlelogfile, "//record") /><table><tr><tdcolspan="2">Month: #left(logfiles.name,3)#</td></tr><CFLOOParray="#records#"index="record"><tr><td>#record.XmlAttributes.name#</td><td>#record.XmlAttributes.spend#</td></tr></CFLOOP></table></CFOUTPUT>

Of course you would need to change that the year comes from FORM-Scope, sum up multiple records for each person and maybe (if you can control it) change the filename from logs to 01-2011,02-2011,03-2011,..12-2011 so that they're getting directly sorted correctly.

Post a Comment for "Parsing Log Files In A Folder In Coldfusion"