Skip to content

Guide to Using Log Levels

Casey Flynn edited this page Dec 18, 2015 · 4 revisions

Use the following log levels to aid in debugging development and production.

  • info as first log in a prototype method. Intended to aid in tracing method call stacks for a given use request or action.
  • trace Used for logging flow throughout a function body. Useful when logging in a narrow focus.
  • error application level error that shouldn't normally ever happen. MongoDB response error for example.
  • warn unexpected but non-application level error. Instance document not found in a query for ex.

Example of proper logging of a method.

/** * Atomic set to starting */InstanceSchema.methods.setStateToStarting=function(cb){//Share a common base object of log data, extend when necessary.varlogData={tx: true,// Include tx: true to make this log message // a part of a request-trace batch of logsinstanceId: this._id,instanceName: this.name,dockerContainer: this.container.dockerContainer};// Use info for an initial log message at the beginning of a method// ### EXAMPLElog.info(logData,'InstanceSchema.methods.setStateToStarting');varself=this;varowner=this.owner;varcreatedBy=this.createdBy;Instance.findOneAndUpdate({'_id': this._id,'container.dockerContainer': this.container.dockerContainer,'container.inspect.State.Starting': {'$exists': false},'container.inspect.State.Stopping': {'$exists': false}},{'$set': {'container.inspect.State.Starting': true}},function(err,result){if(err){varlogErrData=put({err: err},logData);// Use error for application errors// ### EXAMPLElog.error(logErrData,'setStateToStarting fineOneAndUpdate error');returncb(err);}elseif(!result){// Use warn for non-application errors// ### EXAMPLElog.warn(logData,'setStateToStarting fineOneAndUpdate !result');// Fetch instance to determine if it exists, or is starting or stoppingInstance.findOne({_id: self._id,'container.dockerContainer': self.container.dockerContainer},function(err,result2){if(err){varlogErrData=put({err: err},logData);// ### EXAMPLElog.error(logErrData,' fineOneAndUpdate !result findOne error');returncb(err);}elseif(!result2){returncb(Boom.badRequest('instance container has changed'));}// Use trace to assert logic flow reaches key points// ### EXAMPLElog.trace(logData,'setStateToStarting fineOneAndUpdate !result findOne success');cb(null,result2);});}else{log.trace(logData,'setStateToStarting fineOneAndUpdate success');// must preserve owner/createdBy if set via// populateOwnerAndCreatedByresult.owner=owner;result.createdBy=createdBy;returncb(null,result);}});};

Clone this wiki locally