Have you ever wanted to parse through your IIS web logs to create a "WebTrends" like application in ColdFusion?
This tutorial will show you how you can parse through the IIS log files and have variables ready to insert into databases, print or to achieve whatever you want with them. Let's begin:
The first thing we need to do is to define some
default values, these will be used later on in the application.
The values we will specify will be:
Keep in mind that this tutorial is
showing you how to process a single log file at a time, if you want to do
multiple log files at a time a few tweaking changes will be needed. Contact
me for additional help.
<!------------ By default set value ------------>
<cfparam name="file2load"
default="C:\logs\log.txt">
<cfset entry_ws3_number = "ws3srv">
<cfset entry_URL = "www.yoursite.com">
<!------------ Load the file ---------------->
<cffile action="READ"
file="#file2load#"
variable="log_data">
<cfoutput>
<hr>
<!------- Break by line ---------->
<cfloop
index="rc"
list="#log_data#"
delimiters="#chr(13)##chr(10)#">
<!---------- Check that it's an actual line ------->
<cfif #left(rc, 1)# eq
"#chr(35)#">
<!-------- the current line is not a valid
log item, skip and go to the next line ----------->
<cfelse>
<!----------- by item --------->
<CFSET Value = 0>
<cfloop index="id"
list="#rc#"
delimiters="#chr(32)#">
<CFSET FirstValue = "#id#">
<CFSET Value = Value + 1>
<CFIF Value is 1>
<cfset date = "#FirstValue#">
<CFELSEIF Value is 2>
<cfset time = "#FirstValue#">
<CFELSEIF Value is 3>
<cfset c_ip = "#FirstValue#">
<CFELSEIF Value is 4>
<cfset cs_username = "#FirstValue#">
<CFELSEIF Value is 5>
<cfset s_ip = "#FirstValue#">
<CFELSEIF Value is 6>
<cfset s_port = "#FirstValue#">
<CFELSEIF Value is 7>
<cfset cs_method = "#FirstValue#">
<CFELSEIF Value is 8>
<cfset cs_uri_stem = "#FirstValue#">
<CFELSEIF Value is 9>
<cfset cs_uri_query = "#FirstValue#">
<CFELSEIF Value is 10>
<cfset sc_status = "#FirstValue#">
<CFELSEIF Value is 11>
<cfset User_Agent = "#FirstValue#">
<CFELSEIF Value is 12>
<cfset Referer = "#FirstValue#">
<CFELSE>
</cfif>
</cfloop>
<!----------- Display the values --------------->
Date: #date#<BR>
Time: #time#<BR>
Cust_IP: #c_ip#<BR>
Username: #cs_username#<BR>
Site_IP: #s_ip#<BR>
Site_Port: #s_port#<BR>
Method: #cs_method#<BR>
Site_path: #cs_uri_stem#<BR>
Query: #cs_uri_query#<BR>
Status: #sc_status#<BR>
Browser: #User_Agent#<BR>
Referer: #Referer#<HR>
<!------------- Here you may insert into the DB ---------------->
<cfquery name="qInsertEntry"
datasource="iis_logs">
insert into iis_logs(
entry_date,
entry_time,
entry_Cust_IP,
entry_Username,
entry_Site_IP,
entry_Site_Port,
entry_Method,
entry_Site_path,
entry_Query_Strings,
entry_Status,
entry_Browser,
entry_Referer,
entry_ws3_number,
entry_URL
)
values(
#CreateODBCDate(date)#,
#CreateODBCTime(Time)#,
'#c_ip#',
'#cs_username#',
'#s_ip#',
'#s_port#',
'#cs_method#',
'#cs_uri_stem#',
'#cs_uri_query#',
'#sc_status#',
'#User_Agent#',
'#Referer#',
'#entry_ws3_number#',
'#entry_URL#'
)
</cfquery>
</cfif>
</cfloop>
</cfoutput>
It's that simple, you are reading and processing the logs, you can now query the "iis_logs" database to create reports and to let your users know how many visitors, hits and page views you've had this week! I suggest you schedule this tag and let it run once per day reading your logs for that day, that way you will have all this data in your database for easy access at any time!
Added 01/02/2003 (Pablo Varando):
I've been asked by some of our users to explain how you would process this for
multiple files in a directory, so I wanted to write an addition to this tutorial
with more information about that, so here we go!
<!------------ By default set value ------------>
<cfparam name="LogsDir"
default="C:\logs\">
<cfset entry_ws3_number = "ws3srv">
<cfset entry_URL = "www.yoursite.com">
<!--- Do a CFDirectory to
retrieve the log files **ONLY** --->
<cfdirectory action="list"
directory="#LogsDir#"
name="LogFiles"
filter="*.log">
<!--- Loop through the
directory and process each file --->
<cfloop query="LogFiles">
<!--- Specify the
filename to a variable --->
<cfset file2load = "#LogsDir##Name#">
<!------------ Load the file ---------------->
<cffile action="READ"
file="#file2load#"
variable="log_data">
<cfoutput>
<hr>
<!------- Break by line ---------->
<cfloop
index="rc"
list="#log_data#"
delimiters="#chr(13)##chr(10)#">
<!---------- Check that it's an actual line ------->
<cfif #left(rc, 1)# eq
"#chr(35)#">
<!-------- the current line is not a valid
log item, skip and go to the next line ----------->
<cfelse>
<!----------- by item --------->
<CFSET Value = 0>
<cfloop index="id"
list="#rc#"
delimiters="#chr(32)#">
<CFSET FirstValue = "#id#">
<CFSET Value = Value + 1>
<CFIF Value is 1>
<cfset date = "#FirstValue#">
<CFELSEIF Value is 2>
<cfset time = "#FirstValue#">
<CFELSEIF Value is 3>
<cfset c_ip = "#FirstValue#">
<CFELSEIF Value is 4>
<cfset cs_username = "#FirstValue#">
<CFELSEIF Value is 5>
<cfset s_ip = "#FirstValue#">
<CFELSEIF Value is 6>
<cfset s_port = "#FirstValue#">
<CFELSEIF Value is 7>
<cfset cs_method = "#FirstValue#">
<CFELSEIF Value is 8>
<cfset cs_uri_stem = "#FirstValue#">
<CFELSEIF Value is 9>
<cfset cs_uri_query = "#FirstValue#">
<CFELSEIF Value is 10>
<cfset sc_status = "#FirstValue#">
<CFELSEIF Value is 11>
<cfset User_Agent = "#FirstValue#">
<CFELSEIF Value is 12>
<cfset Referer = "#FirstValue#">
<CFELSE>
</cfif>
</cfloop>
<!----------- Display the values --------------->
Date: #date#<BR>
Time: #time#<BR>
Cust_IP: #c_ip#<BR>
Username: #cs_username#<BR>
Site_IP: #s_ip#<BR>
Site_Port: #s_port#<BR>
Method: #cs_method#<BR>
Site_path: #cs_uri_stem#<BR>
Query: #cs_uri_query#<BR>
Status: #sc_status#<BR>
Browser: #User_Agent#<BR>
Referer: #Referer#<HR>
<!------------- Here you may insert into the DB ---------------->
<cfquery name="qInsertEntry"
datasource="iis_logs">
insert into iis_logs(
entry_date,
entry_time,
entry_Cust_IP,
entry_Username,
entry_Site_IP,
entry_Site_Port,
entry_Method,
entry_Site_path,
entry_Query_Strings,
entry_Status,
 
This is a brief demonstration on how to use Fusebox 2.0 Methodology.
Author: Pablo Varando
Views: 21,134
Posted Date: Friday, September 6, 2002
The following tutorial will briefly touch over Custom Tags and show you what they are, how you use them, and how they benfit you by using them.
Author: Pablo Varando
Views: 25,160
Posted Date: Friday, September 6, 2002
Learn how to create a contact page in ColdFusion.
Author: Pablo Varando
Views: 34,676
Posted Date: Tuesday, August 13, 2002
This tutorial will demonstrate how to alternate row colors when outputing your data.
Author: Pablo Varando
Views: 34,240
Posted Date: Tuesday, September 17, 2002
This tutorial will show you how you can add smiles to your messages on the fly!
Author: Pablo Varando
Views: 22,947
Posted Date: Tuesday, October 29, 2002
This tutorial will demonstrate how to verify users passwords to be CaSe SensiTive so add another layer of security to your applications!
Author: Pablo Varando
Views: 51,666
Posted Date: Wednesday, February 5, 2003
This tutorial is not ColdFusion oriented, but covers a great trick to allow you to submit a single form to a variety of different pages on the fly.
Author: Pablo Varando
Views: 17,882
Posted Date: Monday, December 1, 2003
This tutorial will demonstrate how to clear your applications sessions variables with just three lines of code!
Author: Pablo Varando
Views: 27,523
Posted Date: Friday, October 4, 2002
This tutorial will demonstrate how to use .INI files with ColdFusion. Perfect for users wishing to create administration areas for existing software titles that are INI file driven (i.e. FTP Servers).
Author: Pablo Varando
Views: 21,348
Posted Date: Friday, October 4, 2002
This tutorial will demonstrate how to create a query from two different queries based from two separate datasources. This is the easiest way to combine your data.
Author: Pablo Varando
Views: 22,445
Posted Date: Monday, March 10, 2003
This tutorial will demonstrate how to correctly serve documents via ColdFusion and allow you to correctly name the download as you see fit!
Author: Pablo Varando
Views: 16,199
Posted Date: Tuesday, February 10, 2004
Have you ever wanted to display a count of how many people are on your web site at any given moment? This tutorial will demonstrate to you how to achieve just that. It will count your web site's active sessions and allow you to display them to your visitors.
Author: Pablo Varando
Views: 31,895
Posted Date: Sunday, August 25, 2002
This tutorial will show you how to make a file content crawler with ColdFusion to find specified documents in a folder and its children folders. (Similar to find files or folder in Windows(c) Operating Systems 'find' feature).
Author: Pablo Varando
Views: 27,112
Posted Date: Saturday, July 19, 2003
This tutorial will show you how to create a fully automated system to allow visitors to subscribe and unsubscribe to your newsletter, and for administrators to send out a newsletter to all the registered users.
Author: Pablo Varando
Views: 30,961
Posted Date: Friday, September 6, 2002
This tutorial will demonstrate how you can create a "member's only" area. It will show you how to log them in and how to check that they are logged in.
Author: Pablo Varando
Views: 78,698
Posted Date: Monday, August 19, 2002
This tutorial will show you how to create an ODBC (Database) connection from within your ColdFusion MX Administration Area.
Author: Pablo Varando
Views: 31,070
Posted Date: Monday, January 6, 2003
Have you ever wanted to create your very own RSS XML News Feeds? This tutorial will show you how to create an RSS feed that will allow you to syndicate your web site and allow the world to easily use your data!
Author: Pablo Varando
Views: 31,573
Posted Date: Thursday, January 15, 2004
This tutorial will show you how to create, modify and delete database tables easily with ColdFusion.
Author: Pablo Varando
Views: 27,297
Posted Date: Monday, October 14, 2002
This tutorial will demonstrate how you can delete all files and sub-folders in a specified folder using ColdFusion and Windows!
Author: Pablo Varando
Views: 17,223
Posted Date: Wednesday, September 7, 2005
This tutorial will demonstrate how to delete records from a database via your website using ColdFusion.
Author: Pablo Varando
Views: 27,137
Posted Date: Friday, July 4, 2003
This tutorial will show you how to you can provide your members with the ability to save their username and password into memory, so they dont have to type it in everytime the want to log in to your web site.
Author: Pablo Varando
Views: 23,440
Posted Date: Tuesday, May 13, 2003
Learn how to create database connection, by skipping the old ODBC connections with ColdFusion.
Author: Pablo Varando
Views: 25,126
Posted Date: Friday, August 16, 2002
This tutorial will demonstrate how to display the date a web page was last modified to your visitors dynamically.
Author: Pablo Varando
Views: 14,471
Posted Date: Monday, April 12, 2004
This tutorial will demonstrate how you can get the size of a folder (and sub folders) using ColdFusion and Windows File System Object (FSO).
Author: Pablo Varando
Views: 19,945
Posted Date: Tuesday, April 8, 2003
This tutorial will demonstrate how you can use the functions that come built in on your database to do the work, instead of doing the work in your code the hard way!
Author: Pablo Varando
Views: 27,762
Posted Date: Thursday, August 8, 2002
This tutorial will show you two two ways you can implement error checking, to ensure that your users are actually entering the required fields on your forms!
Author: Pablo Varando
Views: 21,621
Posted Date: Wednesday, October 16, 2002
This tutorial will show you how to insert data into a database, then have that information emailed to you and the person submitting the data.
Author: Pablo Varando
Views: 40,762
Posted Date: Thursday, August 1, 2002
This tutorial will demonstrate how you can use one form a user fills out to insert into multiple database tables.
Author: Pablo Varando
Views: 25,797
Posted Date: Tuesday, October 15, 2002
This tutorial will show you how to load your images from an actual .cfm page. Therefore, allowing you to prevent people from using your content on their web sites.
Author: Pablo Varando
Views: 29,739
Posted Date: Friday, March 14, 2003
This tutorial demonstrate how to implement "Previous" & "Next" into your existing results page.
Author: Pablo Varando
Views: 32,778
Posted Date: Tuesday, September 17, 2002
This tutorial will demonstrate how use ColdFusion, Javascript and Style sheets to create the perfect Printing Machine! ;)
Author: Pablo Varando
Views: 26,682
Posted Date: Sunday, December 15, 2002
This tutorial will show you how to parse XML files (RSS Feeds) with ColdFusion MX and it uses an EasyCFM.COM Feed for example [Feed: 5 Most Viewed Tutorials]. It shows you how to call it via CFHTTP all the way to parse and display your records!
Author: Pablo Varando
Views: 26,503
Posted Date: Saturday, December 27, 2003
This tutorial will show you how you can parse through your IIS (web server) log files and insert the values into a database, therefore allowin you to display real-time stats to your visitors (i.e hits this week, etc..)
Author: Pablo Varando
Views: 29,948
Posted Date: Monday, November 4, 2002
This is the basics of ColdFusion. This tutorial will demonstrate how to query a database and then display the records found.
Author: Pablo Varando
Views: 32,759
Posted Date: Saturday, August 3, 2002
This tutorial will demonstrate how to send out multiple attachments with
Author: Pablo Varando
Views: 35,959
Posted Date: Friday, October 11, 2002
Learn how to use User-Defined Functions in ColdFusion 5.0.
Author: Pablo Varando
Views: 19,694
Posted Date: Wednesday, August 21, 2002
This tutorial will show you how to create a mail system for your site. It will allow you to get your email from a POP3 server, view your inbox, then view the message (with attachments), reply and delete that message as well.
Author: Pablo Varando
Views: 29,393
Posted Date: Thursday, November 7, 2002
This tutorial will show you how to use arrays to display data properly in ColdFusion.
Author: Pablo Varando
Views: 25,737
Posted Date: Monday, October 28, 2002
This tutorial is intended to show you how to use the ColdFusion tag <CFRegistry>. This tutorial will show you how to add your current IP to the IP Debug List in the ColdFusion Administrator.
Author: Pablo Varando
Views: 23,842
Posted Date: Wednesday, November 6, 2002
This tutorial will demonstrate how to implement the PayPal IPN (Instant Payment Notification) into your e-commerce applications to accept credit cards in real time!
Author: Pablo Varando
Views: 64,247
Posted Date: Wednesday, September 25, 2002
This tutorial will demonstrate how to use query string values instead of form values.
Author: Pablo Varando
Views: 36,043
Posted Date: Sunday, September 15, 2002
This tutorial will demonstrate how you can get the ID of the record you have just inserted without having to connect to the database again!
Author: Pablo Varando
Views: 22,285
Posted Date: Monday, August 11, 2003