One thing i miss in Flash, is a way to log data in my application. It might be in there somewhere, but i couldn’t manage to find it. So to make myself usefull, i wrote a simple (very simple) LogManager class.
What it does is ease the way you normally trace any events you want to keep an eye on to the output window. It also has some sort of support for logging to an php/asp script eventually. This way you can create logs into txt files and review them at a later time, after your visitors have encountered any logged event.
You can download the zipfile in the resources section.
Here is the code:
/* * Author: Ben Smeets * Date: 02/07/2004 * Original Filename: CLogManager.as * Description: Makes formatted tracing and logging * easy :) */; class nl.flashZone.CLogManager{ private var _intTreshold : Number; private var _blnUseTimestamps : Boolean; private var _strLogUrl : String; // constructor public function CLogManager () { _intTreshold = 0; // show all logs by default _blnUseTimestamps = true; // show timestamps by default _strLogUrl = ""; // do not send logdata to an url by default }; // getters/setters public function set Treshold (num : Number) : Void { _intTreshold = num; }; public function set UseTimestamps (bln : Boolean) : Void { _blnUseTimestamps = bln; }; public function set LogUrl (str : String) : Void { _strLogUrl = str; }; public function get LogUrl () : String { return _strLogUrl; }; // public functions public function logDebug (str : String) : Void { if (str.length != 0 && _intTreshold < 1) { output ("[DEBUG" + timeStamp () + "]: \t\t" + str); }; }; public function logInfo (str : String) : Void { if (str.length != 0 && _intTreshold < 2) { output ("[INFO" + timeStamp () + "]: \t\t" + str); }; }; public function logError (str : String) : Void { if (str.length != 0 && _intTreshold < 3) { output ("[ERROR!" + timeStamp () + "]: \t\t" + str); }; }; // private functions private function timeStamp () : String { if ( ! _blnUseTimestamps); return ""; var tDate = new Date (); return " " + tDate.getDate () + "/" + tDate.getMonth () + "/" + tDate.getFullYear () + " " + tDate.getHours () + ":" + tDate.getMinutes () + ":" + tDate.getSeconds () + "." + tDate.getMilliseconds () + " "; }; private function output (str : String) : Void { // we can always trace logdata to output window trace (str); // we can also choose to log to an url if (_strLogUrl != "") { loadVariables (_strLogUrl + "?Log=" + str, "GET"); }; };};
Example usage:
import nl.flashZone.CLogManager; var gLog:CLogManager = new CLogManager(); // show date/timestamp per line yes or no (try difference)gLog.UseTimestamps = false; // what levels to show?// 0 = all// 1 = only logInfos and logErrors// 2 = only logErrorsgLog.Treshold = 0; _root.onEnterFrame = function() { gLog.logDebug("You see! nice pretty log line."); gLog.logInfo("You see! nice pretty log line."); gLog.logError("You see! nice pretty log line.");}
Leave a reply
You must be logged in to post a comment.