Removing trace() from your code is a great way to optimize it and improve overall performance. This is easy enough to do in Flash by checking “Omit trace actions” in the Publish Settings window, but in Flex there is no such setting or compiler argument that I’ve seen. No worries, there is a solution.
Luckily Flex provides us with a logging framework in the mx.logging package. To implement logging within your application you have to specify targets that will get the log messages. One of the available targets is the TraceTarget. When you want to omit trace statements, just comment out the Log.addTarget call and that’s it. See the example below on how to implement tracing with the Flex logger.
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="init();"> <mx:Script> <![CDATA[ import mx.logging.Log; import mx.logging.ILogger; import mx.logging.LogEventLevel; import mx.logging.targets.TraceTarget; private var _log:ILogger; private var _traceTarget:TraceTarget; private function init():void { _traceTarget = new TraceTarget(); _traceTarget.filters=["*"]; _traceTarget.level = LogEventLevel.ALL; _traceTarget.includeDate = false; _traceTarget.includeTime = false; _traceTarget.includeCategory = true; _traceTarget.includeLevel = true; Log.addTarget(_traceTarget); _log = Log.getLogger("ExampleLogger"); _log.info("Your tracing like a pro!"); } ]]> </mx:Script> </mx:Application>