Logging in ActionScript
Here’s the translation of the Go logging example to ActionScript, formatted in Markdown suitable for Hugo:
ActionScript does not have built-in logging capabilities as robust as Go’s log and slog packages. However, we can create a simple logging system using the trace() function and custom classes. Here’s an example of how we might implement logging in ActionScript:
package {
    import flash.display.Sprite;
    import flash.utils.getTimer;
    import flash.system.Capabilities;
    public class LoggingExample extends Sprite {
        public function LoggingExample() {
            // Simple logging using trace()
            trace("standard logger");
            // Custom logger with timestamps
            var logger:Logger = new Logger();
            logger.log("with timestamp");
            // Custom logger with file/line information
            logger.setIncludeFileInfo(true);
            logger.log("with file/line");
            // Custom logger with prefix
            var myLogger:Logger = new Logger("my:");
            myLogger.log("from myLogger");
            // Change prefix
            myLogger.setPrefix("ohmy:");
            myLogger.log("from myLogger");
            // Logging to a custom output (StringBuffer in this case)
            var bufLogger:BufferLogger = new BufferLogger("buf:");
            bufLogger.log("hello");
            trace("from bufLogger:", bufLogger.getBuffer());
            // JSON-like structured logging
            var structLogger:StructuredLogger = new StructuredLogger();
            structLogger.info("hi there");
            structLogger.info("hello again", {key: "val", age: 25});
        }
    }
}
class Logger {
    private var prefix:String;
    private var includeFileInfo:Boolean;
    public function Logger(prefix:String = "") {
        this.prefix = prefix;
        this.includeFileInfo = false;
    }
    public function log(message:String):void {
        var output:String = getTimestamp() + " " + prefix + message;
        if (includeFileInfo) {
            output = getFileInfo() + " " + output;
        }
        trace(output);
    }
    public function setPrefix(newPrefix:String):void {
        prefix = newPrefix;
    }
    public function setIncludeFileInfo(include:Boolean):void {
        includeFileInfo = include;
    }
    private function getTimestamp():String {
        var date:Date = new Date();
        return date.toLocaleString();
    }
    private function getFileInfo():String {
        return "[" + Capabilities.version + "]";
    }
}
class BufferLogger extends Logger {
    private var buffer:String;
    public function BufferLogger(prefix:String = "") {
        super(prefix);
        buffer = "";
    }
    override public function log(message:String):void {
        buffer += getTimestamp() + " " + prefix + message + "\n";
    }
    public function getBuffer():String {
        return buffer;
    }
}
class StructuredLogger {
    public function info(message:String, data:Object = null):void {
        var output:Object = {
            time: new Date().toLocaleString(),
            level: "INFO",
            msg: message
        };
        
        if (data) {
            for (var key:String in data) {
                output[key] = data[key];
            }
        }
        
        trace(JSON.stringify(output));
    }
}This ActionScript code demonstrates logging concepts similar to those in the original example:
- We use 
trace()for basic logging. - We create a 
Loggerclass that adds timestamps and prefixes to log messages. - The 
Loggerclass can include file information (in this case, just the Flash Player version). - We demonstrate changing the prefix of a logger.
 - We create a 
BufferLoggerthat logs to a string buffer instead of directly to the console. - We create a 
StructuredLoggerthat outputs JSON-like structured log messages. 
To run this code, you would typically compile it into a SWF file and run it in a Flash Player or AIR runtime environment. The output would appear in the Flash debugger console or trace log, depending on your development setup.
Note that ActionScript doesn’t have built-in JSON support, so you might need to use a third-party library for JSON stringification in a real application. Also, the file and line information is simulated here, as ActionScript doesn’t provide easy access to this information at runtime.
This example provides a basic framework for logging in ActionScript, which you can expand upon for more complex logging needs in your ActionScript applications.