Remove log calls from (production) code

Is your code packed with Ti.API. and console. calls? Good! They are really useful to inspect and debug your app. But once in production only error-level messages end up in the console for iOS, whereas Android only filters debug-calls out. So for messages you don’t want to show in production use Ti.API.debug and for messages that should use Ti.API.error. Be aware of this in particular when logging sensitive information.

An alternative: ti-stealth

I just pushed a small NodeJS CLI, CommonJS module and example alloy.jmk to help you deal with stripping log calls from production: ti-stealth.

Drop the included example alloy.jmk in your Alloy app’s app folder or merge it with your existing file’s post:compile hook:

task("post:compile", function(event, logger) {
  if (event.alloyConfig.deployType === 'production') {
    require('ti-stealth').enable(event.dir.resources, {
      notLevels: 'error'
    });
  }
});

After Alloy has compiled to Resources, Stealth will replace all non-error log calls like:

// before:
Ti.API.info('hello');

// after:
(function(){/*Ti.API.info*/})('hello');

You can safely run the Stealth CLI ($ ti-stealth enable <path>) on classic Titanium projects as well, since the original logging method is preserved in comments can be restored using $ ti-stealth restore <path>.

App imagineer: Imagining, Engineering & Speaking about Native mobile Apps with Appcelerator Titanium & Alloy • Meetup organizer • Certified Expert • Titan


Comments