Removing Trace and Logging Statements for Production

 Nov, 06 - 2010   3 comments   Uncategorized

I’m saving these recipes here so I can bookmark, and I’m sure someone else will find them useful.

I decided to convert calls to trace or my logging framework to use conditional compilation so that they won’t be included when I do a production build, but are included when I do a debug build.

The Flex compiler supports this by allowing you to set a variable at compile-time, eg:


In your code, simply wrap the code to be conditionally compiled in curly braces, prepended with this variable, eg:

	trace( 'Debug' );

I spent some time writing a regular expression that finds trace statements, and wraps them in this block. In plain English, start at the beginning of the line, match zero or more whitespace, the word “trace”, zero or more whitespace, an open parenthesis, the rest of the line.

Search for: ^([ \t]*)(trace[ \t]*\(.*)$
Replace with: $1CONFIG::debug { $2 }

FDT’s internal parser doesn’t like the conditional compilation syntax, so we need to change our replacement text slightly to wrap that part in markers instructing it to ignore them.

Search for: ^([ \t]*)(trace[ \t]*\(.*)$
Replace with: $1/*FDT_IGNORE*/ CONFIG::debug /*FDT_IGNORE*/ { $2 }

The same recipe for any call to logger, eg: logger.debug,, logger.warn, etc.

Search for: ^([ \t]*)(logger\..*)$
Replace with: $1/*FDT_IGNORE*/ CONFIG::debug /*FDT_IGNORE*/ { $2 }

Related articles

 Comments 3 comments

  • Joa Ebert says:

    you are right about conditional statements. They are very powerful and can be used for even more.
    However the latest Flex SDK has the option -compiler.omit-trace-statements which is by default true for release builds and omits trace statements. That implementation has some problems since it removes any method named “trace” and not the one that is TopLevel::trace.
    Another option which automates this task for you is using Apparat’s stripper tool. This one removes all TopLevel::trace statements in the bytecode keeping side effects.

  • jon jon says:

    Joa, humbled to have your comments. I’d been vaguely aware of Apparat’s stripper tool, but had forgotten where to find it. I’d specifically been wondering about that detail of -compiler.omit-trace-statements — thanks for posting those.

    In my specific case, I’m using a custom logger, so the conditional route is probably my only choice — that or digging into Apparat!


  • Leave a Reply

    Your email address will not be published. Fields with * are mandatory.