Monday, November 08, 2010 at 12:06 AM.

system.verbs.builtins.webserver.postFilters.commonLog

on commonLog (pta, flDailyRoll=false) {
	<<This post-filter writes a log file in Common Log format of all the hits on the server.
		<<March 04, 1998 at 6:02:36 PM by WMF
		<<Change Notes:
			<<December 11, 1999 at 12:33:49 AM by AR
				<<If user.webserver.prefs.logFile is not defined, don't attempt to coerce the return value (a boolean) of webserver.getPref to a filespec. This bug was responsible for producing a stream of postfilter error messages in the webserver error log.
			<<07/10/00; 5:44:18 PM by PBS
				<<flDailyRoll is a new optional parameter. If true, then log files are stored in Guest Databases/ops/logs/commonLog/. A new log file is created each day. The filenames are of the form 2000-07-10.txt -- yyyy-mm-dd.txt.
				<<If flDailyRoll is false or not supplied, then the script works exactly the same as it always did.
			<<12/6/01; 9:29:53 PM by PBS
				<<New prefs: user.webserver.prefs.commonLog.flUseCommonLogDateFormat, flIncludeVirtualHosts, flIncludeReferers, flIncludeUserAgents. Controls date format, virtual hosts, referers, and user agents, respectively.
	local (f);
	local (flUseCommonLogDateFormat = false);
	local (flIncludeVirtualHosts = false);
	local (flIncludeReferers = false);
	local (flIncludeUserAgents = false);
	
	bundle { //PBS 12/06/01: get prefs
		if defined (user.webserver.prefs.commonLog.flUseCommonLogDateFormat) {
			flUseCommonLogDateFormat = user.webserver.prefs.commonLog.flUseCommonLogDateFormat};
		if defined (user.webserver.prefs.commonLog.flIncludeVirtualHosts) {
			flIncludeVirtualHosts = user.webserver.prefs.commonLog.flIncludeVirtualHosts};
		if defined (user.webserver.prefs.commonLog.flIncludeReferers) {
			flIncludeReferers = user.webserver.prefs.commonLog.flIncludeReferers};
		if defined (user.webserver.prefs.commonLog.flIncludeUserAgents) {
			flIncludeUserAgents = user.webserver.prefs.commonLog.flIncludeUserAgents}};
	
	bundle { //get the path to the current log file
		if flDailyRoll { //PBS 07/10/00: write to a different text file every day
			local (folder = config.mainResponder.prefs.logFolder);
			folder = folder + "Common Log" + file.getPathChar ();
			file.sureFilePath (folder);
			file.sureFolder (folder);
			local (day, month, year, hour, minute, second);
			date.get (clock.now (), @day, @month, @year, @hour, @minute, @second);
			local (fname = year + "-" + string.padWithZeros (month, 2) + "-" + string.padWithZeros (day, 2) + ".txt");
			
			f = folder + fname}
		else { //traditional behavior -- log file path is specified by user.webserver.prefs.logFile
			f = webserver.getPref ("logFile");
			if typeOf (f) == booleanType { //no log file pref has been set up
				return (false)}}};
	
	bundle { //write to the log file
		if not file.exists (f) {
			file.sureFilePath (f);
			file.new (f);
			if system.environment.isMac {
				file.setType (f, 'TEXT');
				file.setCreator (f, 'R*ch')}};
		
		local (dateString);
		if flUseCommonLogDateFormat { //PBS 12/06/01: use common log date format
			local (day, month, year, hour, minute, second);
			date.get (clock.now (), @day, @month, @year, @hour, @minute, @second);
			dateString = string.padWithZeros (day, 2) + "/";
			dateString = dateString + string.mid (date.monthToString (month), 1, 3) + "/" + year + ":";
			dateString = dateString + string.padWithZeros (hour, 2) + ":" + string.padWithZeros (minute, 2) + ":" + string.padWithZeros (second, 2) + " ";
			local (timeZone = date.getCurrentTimeZone ());
			timeZone = timeZone / 60; timeZone = timeZone / 60;
			local (flNegative = (timeZone < 0));
			timeZone = string.popLeading (timeZone, "-");
			timeZone = string.popLeading (timeZone, "+");
			timeZone = string.padWithZeros (string (timeZone), 2);
			if flNegative {
				timeZone = "-" + timeZone}
			else {
				timeZone = "+" + timeZone};
			timeZone = timeZone + "00";
			dateString = dateString + timeZone}
		else {
			dateString = date.netStandardString (clock.now ())};
		
		local (line = "");
		
		if flIncludeVirtualHosts { //PBS 12/06/01: add virtual hosts
			if defined (pta^.host) {
				line = pta^.host + " "}
			else {
				line = "- "}};
		
		with pta^ {
			line =  line + client + " - - [" + dateString + "] \"" + firstLine + "\" " + code + " " + sizeOf (responseBody)};
		
		if flIncludeReferers { //PBS 12/06/01: add referer
			line = line + " ";
			if defined (pta^.requestHeaders.referer) and pta^.requestHeaders.referer != "" {
				line = line + "\"" + pta^.requestHeaders.referer + "\""}
			else {
				line = line + "-"}};
		if flIncludeUserAgents { //PBS 12/06/01: user-agent
			line = line + " ";
			if defined (pta^.requestHeaders.["user-Agent"]) and pta^.requestHeaders.["user-Agent"] != "" {
				line = line + "\"" + pta^.requestHeaders.["user-Agent"] + "\""}
			else {
				line = line + "-"}};
		
		semaphore.lock (f, 600);
		file.writeLine (f, line); //writeline automatically opens and closes the file
		semaphore.unlock (f)};
	
	return (true)}



This listing is for code that runs in the OPML Editor environment. I created these listings because I wanted the search engines to index it, so that when I want to look up something in my codebase I don't have to use the much slower search functionality in my object database. Dave Winer.