What are the best practices to log an error?

后端 未结 8 719
野的像风
野的像风 2020-12-29 07:47

Many times I saw logging of errors like these:

System.out.println(\"Method aMethod with parameters a:\"+a+\" b: \"+b);
print(\"Error in line 88\");


        
相关标签:
8条回答
  • 2020-12-29 08:41

    The easiest way to log errors in a consistent format is to use a logging framework such as Log4j (assuming you're using Java). It is useful to include a logging section in your code standards to make sure all developers know what needs to be logged. The nice thing about most logging frameworks is they have different logging levels so you can control how verbose the logging is between development, test, and production.

    0 讨论(0)
  • 2020-12-29 08:42

    Some suggested best-practices

    • Use a logging framework. This will allow you to:

      • Easily change the destination of your log messages
      • Filter log messages based on severity
      • Support internationalised log messages
    • If you are using java, then slf4j is now preferred to Jakarta commons logging as the logging facade.

    • As stated slf4j is a facade, and you have to then pick an underlying implementation. Either log4j, java.util.logging, or 'simple'.

    • Follow your framework's advice to ensuring expensive logging operations are not needlessly carried out

    0 讨论(0)
提交回复
热议问题