A.6. Logging with the File API

The ability to log errors is common to desktop applications and can be very helpful when offering support for your applications. Since the File and FileStream classes give us the ability to write to the local file system, we can use these classes to log any application errors. In this example, we will set the application to log any SQL errors that occur when using the DataManager class. You can create additional log files for errors occurring when there are other types of exceptions thrown.

To accomplish this, we will simply need to update the DataManager.as class with the following steps.

Add the following import statements to the DataManager.as class needed for writing to the file system.

import flash.filesystem.File;
import flash.filesystem.FileStream;
import flash.filesystem.FileMode;

Instantiate the new log file by adding a class variable to the DataManager.as class.

private var sqlLog:File =
File.applicationStorageDirectory.resolvePath("SQLErrorLog.log");

Finally, add the following to the errorHandler() function that is called on any error within the DataManager class. Note that the file is open with FileMode.APPEND as the mode argument. This allows us to append to the file without losing previous log information.

var fileStream:FileStream = new FileStream();
    fileStream.open(sqlLog, FileMode.APPEND);
    fileStream.writeUTFBytes(new Date() + ': ' +  err+'\n\n');
    fileStream.close();

Running the application will not show this new error logging in ...

Get Beginning Adobe® AIR™: Building Applications for the Adobe Integrated Runtime now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.