Skip to content

Instantly share code, notes, and snippets.

@srayhan
Created August 19, 2011 18:14
Show Gist options
  • Save srayhan/1157557 to your computer and use it in GitHub Desktop.
Save srayhan/1157557 to your computer and use it in GitHub Desktop.
Effective logging example
#This is an example code showing a good mix of 4 levels (error, warn, info, and debug) of log messages
#follow the comments to understand the motivation behind each log messages
#please ignore the business logic or coding style, this is only meant to showcase logging only...:-)
#Language used is Ruby
def descope_multiple
begin
logger.debug("entering debug_multiple()...")
wis_to_descope = @project.work_items.find(params[:id])
@wis_descoped = []
@wis_not_descoped = []
if wis_to_descope.present?
wis_to_descope.each do |wi|
begin
#detailed flow captured at debug level
logger.debug("descoping wi #{wi.id}.")
wi.descope(logged_in_user().id)
logger.debug("wi #{wi.id} descoped.")
@wis_descoped << wi
rescue Exception => ex
#it is recoverable since we might be able to desoce other items
logger.warn("cannot descope wi #{wi.id}- #{ex.inspect}")
@wis_not_descoped << wi
end
end
@message = "Successfully descoped items #{@wis_descoped.map(&:canonical_id).inspect}." if @wis_descoped.present?
@message = "#{@message} Could not descope #{@wis_not_descoped.map(&:canonical_id).inspect}." if @wis_not_descoped.present?
#summarizing the action as an info message
logger.info("#{@message}")
logger.debug("moving descoped items #{@wis_descoped.map(&:canonical_id).inspect} items to next sprint")
@project.move_items_to_next_sprint(@wis_descope)
logger.debug("moved descoped items #{@wis_descoped.map(&:canonical_id).inspect} items to next sprint")
else
#summarizing the action for alternative flow as an info message
@message = "Sorry, could not find the items selected for descope."
logger.info("#{@message}")
end
rescue Exception => ex
#not recoverable, logged at an error level
@message = "Sorry, could not descope the selected items. Please try again later."
logger.error("#{@message}- #{ex.inspect}")
end
logger.debug("returning from descope_multiple()")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment