What guidelines do you adhere to for writing good logging statements

后端 未结 7 2201
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-24 07:18

I recently found a log statement in my projects codebase that says \"here i am with search parameter==>===========11/30/2008===1====00:00 AM\"

what guidelines do you

7条回答
  •  梦谈多话
    2020-12-24 08:00

    Do not do String concatenation in logging statements, at least debug statements.

    One Java app I was working on a while ago had dismal performance. So we cracked out the profiler to see where the bottlenecks were. Turned out that it was mostly due to String concatenation operations incurred by assembling DEBUG level logging statements that occurred ALL THE TIME inside nested inner loops etc (hah, and to think they got added in the first place to figure out why the performance was so bad!)

    don't do this

    LOGGER.debug("The variable was " + myVariable + " and we are doing " + foo);
    

    Instead do this

    if (LOGGER.isDebugEnabled()) {
      LOGGER.debug("put your debug statement here " + foo + " and " + bar);
    }
    

提交回复
热议问题