Hey all, I've written a basic sql error logging/debugging xbasic function. Basically, it lets me call write2log("Error","This is the error") from anywhere in xbasic. It will then insert a record in a logging table in my database.
It includes the following information:
1) The Ulink of the user that is logged in.
2) The pagename or scriptname that the call occured on or in.
3)The User agent of the browser the user was using
4) The IP Address of the user (check local laws if you are allowed to capture this info)
Then the code for the table (mysql):
It includes the following information:
1) The Ulink of the user that is logged in.
2) The pagename or scriptname that the call occured on or in.
3)The User agent of the browser the user was using
4) The IP Address of the user (check local laws if you are allowed to capture this info)
Code:
'Date Created: 27-Jun-2011 01:34:31 PM 'Last Updated: 23-Jul-2012 01:07:14 PM 'Created By : Sparticuz 'Updated By : Sparticuz FUNCTION write2log as v ( event as c, error as c = "" ) dim cn as SQL::Connection dim flag as l flag = cn.open("") if flag == .f. then goto static_error end if dim args as sql::arguments if variable_exists("Session") then if variable_exists("Session.__protected__ulink") then args.add("ulink",Session.__protected__ulink) else args.add("ulink","Not Logged In") end if else args.add("ulink","localhost") end if args.add("event",event) args.add("desc",error) if variable_exists("Request") then if variable_exists("Request.Variables.__pageName") then args.add("cur_page",Request.Variables.__pageName) else args.add("cur_page",Request.Script_name) end if args.add("user_agent",Request.UserAgent) args.add("ip",Request.Remote_Addr) else args.add("cur_page","localhost") args.add("user_agent","localhost") args.add("ip","127.0.0.1") end if dim sql as c sql = "INSERT INTO gen_log (`ulink`, `event`, `desc`, `cur_page`, `user_agent`, `ip`) VALUES (:ulink,:event,:desc,:cur_page,:user_agent,INET_ATON(:ip));" flag = cn.Execute(sql,args) if flag == .f. then goto static_error end if cn.FreeResult() cn.Close() delete flag delete cn delete sql delete args end static_error: 'The log entry in the DB has failed. 'Lets try a static text file instead 'TODO end END FUNCTION
Code:
CREATE TABLE `gen_log` ( `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,`ulink` CHAR(36) NULL DEFAULT NULL,`event` VARCHAR(50) NULL DEFAULT NULL,`desc` TEXT NULL,`cur_page` VARCHAR(50) NULL DEFAULT NULL,`user_agent` VARCHAR(255) NULL DEFAULT NULL,`ip` INT(10) UNSIGNED NULL DEFAULT NULL COMMENT 'Use INET_ATON("ip") to convert to int and use INET_NTOA(num) to convert to ip.',`modified_date` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,PRIMARY KEY (`id`))COMMENT='Table used for logging purposes'COLLATE='latin1_swedish_ci'ENGINE=InnoDB;
Comment