0

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".

Tyr1on
  • 1,999
  • 2
  • 13
  • 20

1 Answers1

2

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.

sangupta
  • 2,396
  • 3
  • 23
  • 37
  • Does it mean that I should maintain a string in Shared object and then keep sending it to server at a certain interval of time and clearing it since Shared Object also has memory limitation? – Tyr1on Jun 15 '11 at 04:29
  • For flex application, yes! That is the approach I have seen many flex applications as employing. – sangupta Jul 19 '11 at 16:23
  • in order for `mx_internal override function internalLog()` to work, I have to change it to `override mx_internal function internalLog` and add `import mx.core.mx_internal; use namespace mx_internal;` to the header of the class – Hoang Huynh Jul 12 '13 at 11:52
  • 1
    Thanks @HoangHuynh - I have updated the code to include the same. – sangupta Jul 13 '13 at 12:22