Logging to a local file in Flex
I have my application frontend developed in Flex 开发者_如何转开发3. For logging, we are using traces and Logger at times yet we dont have a specific way to store logs in a local file of User's machine.
In fact, what I learned from Adobe livedocs is that flashplayer manages itself all logs in flashlog.txt file.
Is there any other way I can maintain a copy of logs? flashlog.txt gets cleared everytime we perform "Logout".
You have not mentioned whether your application is a desktop application, or a browser based.
In case of a desktop application you can write a new class,
import mx.core.mx_internal;
use namespace mx_internal;
public class LoggingFileTarget extends LineFormattedTarget {
private const DEFAULT_LOG_PATH:String = "C:/mylogfile.txt";
private var log:File;
public function LoggingFileTarget(logFile:File = null) {
if(logFile != null) {
log = logFile;
} else {
log = new File(DEFAULT_LOG_PATH);
}
}
public function get logURI():String {
return log.url;
}
mx_internal override function internalLog(message:String):void {
write(message);
}
private function write(msg:String):void {
var fs:FileStream = new FileStream();
try {
fs.open(log, FileMode.APPEND);
fs.writeUTFBytes(msg + "\n");
fs.close();
} catch(e:Error) {
trace("FATAL:: Unable to write to log file.");
}
}
public function clear():void {
var fs:FileStream = new FileStream();
fs.open(log, FileMode.WRITE);
fs.writeUTFBytes("");
fs.close();
}
}
In case of a browser based application, you can keep writing either to an in-memory string, or to a local shared object. Using a shared local object, keep appending to logs, and then collate via a web call.
精彩评论