logging.adoc 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. = Logging and Monitoring
  2. :author:
  3. :revnumber:
  4. :revdate: 2016/03/17 20:48
  5. :relfileprefix: ../../
  6. :imagesdir: ../..
  7. ifdef::env-github,env-browser[:outfilesuffix: .adoc]
  8. == Logging Like a Newbie
  9. Many developers just use `System.out.println()` to print diagnostic strings to the terminal. The problem with that is that before the release, you have to go through all your code and make certain you removed all these `println()` calls. You do not want your customers to see them, and needlessly worry about ominous outdated debugging diagnostics.
  10. == Logging Like a Pro
  11. Instead of `println()`, use the standard Java logger from `java.util.logging`. It has many advantages for professional game development:
  12. * You tag each message with its *log level*: Severe error, informative warning, etc.
  13. * You can *switch off or on printing of log messages* up to certain log level with just one line of code.
  14. ** During development, you would set the log level to `fine`, because you want all warnings printed.
  15. ** For the release, you set the log level to only report `severe` errors, and never print informative diagnostics.
  16. * The logger message string is *localizable* and can use variables. Optimally, you localize all error messages.
  17. To print comments like a pro, you use the following logger syntax.
  18. . Declare the logger object once per file. In the following code, replace `HelloWorld` by the name of the class where you are using this line.
  19. +
  20. [source,java]
  21. ----
  22. private static final Logger LOGGER = Logger.getLogger(HelloWorld.class.getName());
  23. ----
  24. . Declare the info that you want to include in the message. The variables (here `a, b, c`) can be any printable Java object. +
  25. Example: `Vector3f a = cam.getLocation();`
  26. . Put the variables in a new `Object` array. Refer to the variables as `++{0},{1},{2}++` etc in the message string. Variables are numbered in the order you put them into the `Object` array.
  27. . Add the logger line and specify the log level:
  28. ** Usecase 1: During debugging, a developer uses a warning to remind himself of a bug:
  29. +
  30. [source,java]
  31. ----
  32. LOGGER.log(Level.WARNING, "why is {0} set to {1} again?!",
  33. new Object[]{a , b});
  34. ----
  35. ** Usecase 2: For the release, you inform the customer of a problem and how to solve it.
  36. +
  37. [source,java]
  38. ----
  39. LOGGER.log(Level.SEVERE, "MyGame error: {0} must not be {1} after {2}! Adjust flux generator settings.",
  40. new Object[]{a , b , c});
  41. ----
  42. [IMPORTANT]
  43. ====
  44. As you see in the examples, you should phrase potentially “customer facing errors in a neutral way and offer _a reason and a solution_ for the error (if you don't, it has no value to your customer). If your deveopment team uses WARNINGs as replacement for casual printlns, make sure you deactivate them for the release.
  45. ====
  46. More details about link:http://docs.oracle.com/javase/8/docs/api/java/util/logging/Level.html[Java log levels] here.
  47. == Switching the Logger on and off
  48. In the release version you will deactivate the logging output to the terminal.
  49. To deactivate the default logger for a release, you set the log level to only report `severe` messages:
  50. [source,java]
  51. ----
  52. Logger.getLogger(””).setLevel(Level.SEVERE);
  53. ----
  54. During development or a beta test, you can tune down the default logger, and set the log level to only report ``warning``s:
  55. [source,java]
  56. ----
  57. Logger.getLogger(””).setLevel(Level.WARNING);
  58. ----
  59. To activate full logging, e.g. for debugging and testing, use the `fine` level:
  60. [source,java]
  61. ----
  62. Logger.getLogger(””).setLevel(Level.FINE);
  63. ----
  64. == Advanced Error Handling
  65. When an uncaught exception reaches certain parts of the jME3 system then the default response is to log the error and then exit the application. This is because an error happening every frame will rapidly fill logs with repeated failings and potentially mask or over-write the original cause of the problem or even the application may continue for a while and then suffer other errors caused by the first and make the root cause hard to determine.
  66. This behaviour can be partially modified by overriding the method handleError in SimpleApplication, for example to display a custom message to users, or to provide users with information on how to report a bug or even to change the way that the error is logged.