Terse Trigger.io Logging

Blog Logo

on
12 Feb 2013

read

I've really been digging Trigger.io. We're really rocking our app. There are few areas that Trigger could definitely improve. Debug
logging is one of those. They have their own tool called Catalyst which exports the developer console of the simulator's app to your browser. I haven't
been able to get that to work with out AngularJS app, so I'm stuck with the terminal output...which is pretty terrible. It's impossible to make sense of
it since you're sifting through lines of noise to find the few bits that actually make sense. It's terrible. I won't event give you an example, because
if you're using it, you know how bad it is.

So, let's fix it. We're using Yeoman and running out app outside of Trigger.io most of the time. We're also using AngularJS so we can inject some services
for logging rather than calling the forge.logging.log directly. So let's create that service.

# I don't like having the forge namespace scattered everywhere# just to do logging. This helps with that and lets us just use# logging as an Angular serviceangular.module('nb').service'log',(forge) -># we are exposing all the standard console logging....just don't abuse it. :)returnconsoleifforge.is.web()error = (args) ->args = Array.prototype.slice.call(args)allErrors = _.filterargs,(o) ->o&&oinstanceofErrorifallErrorsf = _.firstallErrors# Forge expects only two parameters to log, message and error. We need# to combine them into a single string. I don't want to give up being# able to pass objects to console.log since that is pretty useful for# inspecting them.message = (args) -># arguments isn't a _real_ array and needs to be converted to one before# joining it as a string.## Todo. It would be nice to format this message a bit. It jumbles together# in the console.Array.prototype.slice.call(args).toString()groups = []group = (name) ->log"▾ #{name}"groups.pushnamegroupEnd = ->groups.pop()log = (message, error, method = forge.logging.log) ->padding = ""_.timesgroups.length,->padding+='| 'forge.logging.log"#{padding}#{message}",errorlog: ->logmessage(arguments),error(arguments)debug: ->logmessage(arguments),error(arguments),forge.logging.debuginfo: ->logmessage(arguments),error(arguments),forge.logging.infowarn: ->logmessage(arguments),error(arguments),forge.logging.warnerror: ->logmessage(arguments),error(arguments),forge.logging.errorcritical: ->logmessage(arguments),error(arguments),forge.logging.criticalgroup: groupgroupCollapsed: groupgroupEnd: groupEnd

So you might be wondering what group does. Let's just say it's awesome. Try it out. It does what you think it should do in the browser
and we just recreated it a bit here. So now we can inject logging and call all the log methods we want.

Now to deal with the output. I create grunt-forge to wrap running forge in order to get
it built into our Yeoman workflow. We just needed to tweak that a bit to strip out the bits we don't care about...most of the time. After a
bit of tweaking, we end up with output that looks like this.

That is some output that is much more useful to me. You can see a bit of the groups at work there too. I'm not entirely proud of grunt-forge as it exists
now, but it's a good start and it makes Trigger's log so much more useful to me.