Skip to content

Instantly share code, notes, and snippets.

@didyhu
Created May 23, 2016 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save didyhu/b528cac25b791226aafa3401cc0c2b9e to your computer and use it in GitHub Desktop.
Save didyhu/b528cac25b791226aafa3401cc0c2b9e to your computer and use it in GitHub Desktop.
Logging best practices

#Logging Best Practices

Always log, and always log in right place, on right level.

Error Unexcepted exceptions happened, should write down the error message and the call stacks.

log.error(ex.getMessage(),ex);

Warn Something dosen't break the process, but worth to log down. eg, In weixin api, when we find the accessToken expires Unexpectedly, we get another new one, the write down a warning.

if(accessToken==null){
  accessToken=getAccessToken();
  log.warn("AccessToken Expires Unexpectedly, got a new one.");
}

INFO Log when there is a side effect process done. eg, user logged in, someting written to the database, some order or payment created or updated.

orderRepository.save(order);
log.info("Saved Order{id:{}, userId:{}, state:{}}", order.getId(), order.getUserId(), order.getState());

DEBUG/TRACE To log some key intermediate variables, to remind the developer what action is taking place

public Boolean login(String code){
  log.trace("Entering login(code:{})",code)
  UserInfo userInfo=weixinAdapter.getUserInfo(code);
  log.debug("Got UserInfo:{}",userInfo)
  boolean subscribe=userInfo.subscribe==1
  log.trace("Leaving login():{}",subscribe);
  return subscribe;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment