logging.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width,initial-scale=1">
  6. <title>Logging and Monitoring :: jMonkeyEngine Docs</title>
  7. <link rel="canonical" href="https://wiki.jmonkeyengine.org/docs/jme3/advanced/logging.html">
  8. <meta name="generator" content="Antora 2.3.3">
  9. <link rel="stylesheet" href="../../../_/css/site.css">
  10. <meta property="og:image" content="https://wiki.jmonkeyengine.org/_/img/iconx128.png">
  11. <meta property="og:description" content="Logging and Monitoring">
  12. <meta property="og:title" content="jMonkeyEngine Docs">
  13. <link rel="stylesheet" href="../../../_/css/site-extra.css">
  14. <link rel="stylesheet" href="../../../_/css/vendor/docsearch.min.css">
  15. <!-- fetched from https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.css -->
  16. <link rel="icon" href="../../../_/img/favicon.ico" type="image/x-icon">
  17. </head>
  18. <body class="article">
  19. <header class="header">
  20. <nav class="navbar">
  21. <div class="navbar-brand">
  22. <a class="navbar-item" href="https://wiki.jmonkeyengine.org">
  23. <img alt="" src="../../../_/img/jme-logo.png" height="32" type="image/x-icon">
  24. </a>
  25. <div class="navbar-item hide-for-print">
  26. <input id="search-input" type="text" placeholder="Search docs">
  27. </div>
  28. <button class="navbar-burger" data-target="topbar-nav">
  29. <span></span>
  30. <span></span>
  31. <span></span>
  32. </button>
  33. </div>
  34. <div id="topbar-nav" class="navbar-menu">
  35. <div class="navbar-end">
  36. <div class="navbar-item theme-switch-wrapper">
  37. <label class="theme-switch" for="checkbox">
  38. <input type="checkbox" id="checkbox" />
  39. <div class="slider round"></div>
  40. </label>
  41. </div>
  42. <a class="navbar-item" href="https://github.com/jmonkeyengine/wiki">Github</a>
  43. </div>
  44. </div>
  45. </nav>
  46. </header>
  47. <div class="body">
  48. <div class="nav-container" data-component="docs" data-version="master">
  49. <aside class="nav">
  50. <div class="panels">
  51. <div class="nav-panel-menu is-active" data-panel="menu">
  52. <nav class="nav-menu">
  53. <h3 class="title"><a href="../../documentation.html">Docs</a></h3>
  54. <ul class="nav-list">
  55. <li class="nav-item" data-depth="0">
  56. <ul class="nav-list">
  57. <li class="nav-item" data-depth="1">
  58. <a class="nav-link" href="../../documentation.html">Getting Started</a>
  59. </li>
  60. <li class="nav-item" data-depth="1">
  61. <a class="nav-link" href="https://javadoc.jmonkeyengine.org/v3.3.2-stable">JavaDoc</a>
  62. </li>
  63. <li class="nav-item" data-depth="1">
  64. <button class="nav-item-toggle"></button>
  65. <a class="nav-link" href="../../jme3.html">jMonkeyEngine 3</a>
  66. <ul class="nav-list">
  67. <li class="nav-item" data-depth="2">
  68. <button class="nav-item-toggle"></button>
  69. <span class="nav-text">Beginner Tutorials</span>
  70. <ul class="nav-list">
  71. <li class="nav-item" data-depth="3">
  72. <a class="nav-link" href="../beginner/hello_simpleapplication.html">Hello SimpleApplication</a>
  73. </li>
  74. <li class="nav-item" data-depth="3">
  75. <a class="nav-link" href="../beginner/hello_node.html">Hello Node</a>
  76. </li>
  77. <li class="nav-item" data-depth="3">
  78. <a class="nav-link" href="../beginner/hello_asset.html">Hello Asset</a>
  79. </li>
  80. <li class="nav-item" data-depth="3">
  81. <a class="nav-link" href="../beginner/hello_main_event_loop.html">Hello Main Event Loop</a>
  82. </li>
  83. <li class="nav-item" data-depth="3">
  84. <a class="nav-link" href="../beginner/hello_input_system.html">Hello Input System</a>
  85. </li>
  86. <li class="nav-item" data-depth="3">
  87. <a class="nav-link" href="../beginner/hello_material.html">Hello Material</a>
  88. </li>
  89. <li class="nav-item" data-depth="3">
  90. <a class="nav-link" href="../beginner/hello_animation.html">Hello Animation</a>
  91. </li>
  92. <li class="nav-item" data-depth="3">
  93. <a class="nav-link" href="../beginner/hello_picking.html">Hello Picking</a>
  94. </li>
  95. <li class="nav-item" data-depth="3">
  96. <a class="nav-link" href="../beginner/hello_collision.html">Hello Collision</a>
  97. </li>
  98. <li class="nav-item" data-depth="3">
  99. <a class="nav-link" href="../beginner/hello_terrain.html">Hello Terrain</a>
  100. </li>
  101. <li class="nav-item" data-depth="3">
  102. <a class="nav-link" href="../beginner/hello_audio.html">Hello Audio</a>
  103. </li>
  104. <li class="nav-item" data-depth="3">
  105. <a class="nav-link" href="../beginner/hello_effects.html">Hello Effects</a>
  106. </li>
  107. <li class="nav-item" data-depth="3">
  108. <a class="nav-link" href="../beginner/hello_physics.html">Hello Physics</a>
  109. </li>
  110. </ul>
  111. </li>
  112. <li class="nav-item" data-depth="2">
  113. <button class="nav-item-toggle"></button>
  114. <span class="nav-text">Intermediate Tutorials</span>
  115. <ul class="nav-list">
  116. <li class="nav-item" data-depth="3">
  117. <button class="nav-item-toggle"></button>
  118. <span class="nav-text">Concepts</span>
  119. <ul class="nav-list">
  120. <li class="nav-item" data-depth="4">
  121. <a class="nav-link" href="../intermediate/best_practices.html">Best Practices</a>
  122. </li>
  123. <li class="nav-item" data-depth="4">
  124. <a class="nav-link" href="../intermediate/simpleapplication.html">Simple Application</a>
  125. </li>
  126. <li class="nav-item" data-depth="4">
  127. <a class="nav-link" href="../features.html">Features</a>
  128. </li>
  129. <li class="nav-item" data-depth="4">
  130. <a class="nav-link" href="../intermediate/optimization.html">Optimization</a>
  131. </li>
  132. <li class="nav-item" data-depth="4">
  133. <a class="nav-link" href="../faq.html">FAQ</a>
  134. </li>
  135. </ul>
  136. </li>
  137. <li class="nav-item" data-depth="3">
  138. <button class="nav-item-toggle"></button>
  139. <span class="nav-text">Math Concepts</span>
  140. <ul class="nav-list">
  141. <li class="nav-item" data-depth="4">
  142. <a class="nav-link" href="../math_for_dummies.html">Math For Dummies</a>
  143. </li>
  144. <li class="nav-item" data-depth="4">
  145. <a class="nav-link" href="../intermediate/math.html">Math</a>
  146. </li>
  147. <li class="nav-item" data-depth="4">
  148. <a class="nav-link" href="../math.html">More Math</a>
  149. </li>
  150. <li class="nav-item" data-depth="4">
  151. <a class="nav-link" href="../rotate.html">Rotate</a>
  152. </li>
  153. <li class="nav-item" data-depth="4">
  154. <a class="nav-link" href="../math_video_tutorials.html">Math Video Tutorials</a>
  155. </li>
  156. </ul>
  157. </li>
  158. <li class="nav-item" data-depth="3">
  159. <button class="nav-item-toggle"></button>
  160. <span class="nav-text">3D Graphics Concepts</span>
  161. <ul class="nav-list">
  162. <li class="nav-item" data-depth="4">
  163. <a class="nav-link" href="../intermediate/multi-media_asset_pipeline.html">Multi-Media Asset Pipeline</a>
  164. </li>
  165. <li class="nav-item" data-depth="4">
  166. <a class="nav-link" href="../scenegraph_for_dummies.html">Scenegraph for Dummies</a>
  167. </li>
  168. <li class="nav-item" data-depth="4">
  169. <a class="nav-link" href="../beginner/hellovector.html">Hello Vector</a>
  170. </li>
  171. <li class="nav-item" data-depth="4">
  172. <a class="nav-link" href="../terminology.html">Terminology</a>
  173. </li>
  174. <li class="nav-item" data-depth="4">
  175. <a class="nav-link" href="../intermediate/how_to_use_materials.html">How to Use Materials</a>
  176. </li>
  177. <li class="nav-item" data-depth="4">
  178. <a class="nav-link" href="../intermediate/transparency_sorting.html">Transparency and Sorting</a>
  179. </li>
  180. <li class="nav-item" data-depth="4">
  181. <a class="nav-link" href="../external/blender.html">Importing from Blender</a>
  182. </li>
  183. <li class="nav-item" data-depth="4">
  184. <a class="nav-link" href="../external/3dsmax.html">Importing from 3DS Max</a>
  185. </li>
  186. </ul>
  187. </li>
  188. </ul>
  189. </li>
  190. </ul>
  191. </li>
  192. <li class="nav-item" data-depth="1">
  193. <a class="nav-link" href="../../logo.html">Logo Usage</a>
  194. </li>
  195. <li class="nav-item" data-depth="1">
  196. <a class="nav-link" href="../../bsd_license.html">License</a>
  197. </li>
  198. <li class="nav-item" data-depth="1">
  199. <a class="nav-link" href="../../github_tips.html">Github Tips</a>
  200. </li>
  201. </ul>
  202. </li>
  203. <li class="nav-item" data-depth="0">
  204. <button class="nav-item-toggle"></button>
  205. <span class="nav-text">SDK</span>
  206. <ul class="nav-list">
  207. <li class="nav-item" data-depth="1">
  208. <a class="nav-link" href="../../sdk.html">jMonkeyEngine SDK</a>
  209. </li>
  210. </ul>
  211. </li>
  212. </ul>
  213. </nav>
  214. </div>
  215. <div class="nav-panel-explore" data-panel="explore">
  216. <div class="context">
  217. <span class="title">Docs</span>
  218. <span class="version">master</span>
  219. </div>
  220. <ul class="components">
  221. <li class="component is-current">
  222. <span class="title">Docs</span>
  223. <ul class="versions">
  224. <li class="version is-current is-latest">
  225. <a href="../../documentation.html">master</a>
  226. </li>
  227. </ul>
  228. </li>
  229. <li class="component">
  230. <span class="title">Wiki UI</span>
  231. <ul class="versions">
  232. <li class="version is-latest">
  233. <a href="../../../wiki-ui/index.html">master</a>
  234. </li>
  235. </ul>
  236. </li>
  237. </ul>
  238. </div>
  239. </div>
  240. </aside>
  241. </div>
  242. <main class="article">
  243. <div class="toolbar" role="navigation">
  244. <button class="nav-toggle"></button>
  245. <nav class="breadcrumbs" aria-label="breadcrumbs">
  246. <ul>
  247. <li><a href="../../documentation.html">Docs</a></li>
  248. <li><a href="logging.html">Logging and Monitoring</a></li>
  249. </ul>
  250. </nav>
  251. <div class="edit-this-page"><a href="https://github.com/jMonkeyEngine/wiki/edit/master/docs/modules/ROOT/pages/jme3/advanced/logging.adoc">Edit this Page</a></div>
  252. </div>
  253. <div class="content">
  254. <article class="doc">
  255. <h1 class="page">Logging and Monitoring</h1>
  256. <div class="sect1">
  257. <h2 id="logging-like-a-newbie"><a class="anchor" href="#logging-like-a-newbie"></a>Logging Like a Newbie</h2>
  258. <div class="sectionbody">
  259. <div class="paragraph">
  260. <p>Many developers just use <code>System.out.println()</code> 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 <code>println()</code> calls. You do not want your customers to see them, and needlessly worry about ominous outdated debugging diagnostics.</p>
  261. </div>
  262. </div>
  263. </div>
  264. <div class="sect1">
  265. <h2 id="logging-like-a-pro"><a class="anchor" href="#logging-like-a-pro"></a>Logging Like a Pro</h2>
  266. <div class="sectionbody">
  267. <div class="paragraph">
  268. <p>Instead of <code>println()</code>, use the standard Java logger from <code>java.util.logging</code>. It has many advantages for professional game development:</p>
  269. </div>
  270. <div class="ulist">
  271. <ul>
  272. <li>
  273. <p>You tag each message with its <strong>log level</strong>: Severe error, informative warning, etc.</p>
  274. </li>
  275. <li>
  276. <p>You can <strong>switch off or on printing of log messages</strong> up to certain log level with just one line of code.</p>
  277. <div class="ulist">
  278. <ul>
  279. <li>
  280. <p>During development, you would set the log level to <code>fine</code>, because you want all warnings printed.</p>
  281. </li>
  282. <li>
  283. <p>For the release, you set the log level to only report <code>severe</code> errors, and never print informative diagnostics.</p>
  284. </li>
  285. </ul>
  286. </div>
  287. </li>
  288. <li>
  289. <p>The logger message string is <strong>localizable</strong> and can use variables. Optimally, you localize all error messages.</p>
  290. </li>
  291. </ul>
  292. </div>
  293. <div class="paragraph">
  294. <p>To print comments like a pro, you use the following logger syntax.</p>
  295. </div>
  296. <div class="olist arabic">
  297. <ol class="arabic">
  298. <li>
  299. <p>Declare the logger object once per file. In the following code, replace <code>HelloWorld</code> by the name of the class where you are using this line.</p>
  300. <div class="listingblock">
  301. <div class="content">
  302. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">private static final Logger LOGGER = Logger.getLogger(HelloWorld.class.getName());</code></pre>
  303. </div>
  304. </div>
  305. </li>
  306. <li>
  307. <p>Declare the info that you want to include in the message. The variables (here <code>a, b, c</code>) can be any printable Java object.<br>
  308. Example: <code>Vector3f a = cam.getLocation();</code></p>
  309. </li>
  310. <li>
  311. <p>Put the variables in a new <code>Object</code> array. Refer to the variables as <code>{0},{1},{2}</code> etc in the message string. Variables are numbered in the order you put them into the <code>Object</code> array.</p>
  312. </li>
  313. <li>
  314. <p>Add the logger line and specify the log level:</p>
  315. <div class="ulist">
  316. <ul>
  317. <li>
  318. <p>Usecase 1: During debugging, a developer uses a warning to remind himself of a bug:</p>
  319. <div class="listingblock">
  320. <div class="content">
  321. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">LOGGER.log(Level.WARNING, "why is {0} set to {1} again?!",
  322. new Object[]{a , b});</code></pre>
  323. </div>
  324. </div>
  325. </li>
  326. <li>
  327. <p>Usecase 2: For the release, you inform the customer of a problem and how to solve it.</p>
  328. <div class="listingblock">
  329. <div class="content">
  330. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">LOGGER.log(Level.SEVERE, "MyGame error: {0} must not be {1} after {2}! Adjust flux generator settings.",
  331. new Object[]{a , b , c});</code></pre>
  332. </div>
  333. </div>
  334. </li>
  335. </ul>
  336. </div>
  337. </li>
  338. </ol>
  339. </div>
  340. <div class="admonitionblock important">
  341. <table>
  342. <tr>
  343. <td class="icon">
  344. <i class="fa icon-important" title="Important"></i>
  345. </td>
  346. <td class="content">
  347. <div class="paragraph">
  348. <p>As you see in the examples, you should phrase potentially “customer facing errors in a neutral way and offer <em>a reason and a solution</em> for the error (if you don&#8217;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.</p>
  349. </div>
  350. </td>
  351. </tr>
  352. </table>
  353. </div>
  354. <div class="paragraph">
  355. <p>More details about <a href="http://docs.oracle.com/javase/8/docs/api/java/util/logging/Level.html">Java log levels</a> here.</p>
  356. </div>
  357. </div>
  358. </div>
  359. <div class="sect1">
  360. <h2 id="switching-the-logger-on-and-off"><a class="anchor" href="#switching-the-logger-on-and-off"></a>Switching the Logger on and off</h2>
  361. <div class="sectionbody">
  362. <div class="paragraph">
  363. <p>In the release version you will deactivate the logging output to the terminal.</p>
  364. </div>
  365. <div class="paragraph">
  366. <p>To deactivate the default logger for a release, you set the log level to only report <code>severe</code> messages:</p>
  367. </div>
  368. <div class="listingblock">
  369. <div class="content">
  370. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">Logger.getLogger(””).setLevel(Level.SEVERE);</code></pre>
  371. </div>
  372. </div>
  373. <div class="paragraph">
  374. <p>During development or a beta test, you can tune down the default logger, and set the log level to only report <code>warning</code>s:</p>
  375. </div>
  376. <div class="listingblock">
  377. <div class="content">
  378. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">Logger.getLogger(””).setLevel(Level.WARNING);</code></pre>
  379. </div>
  380. </div>
  381. <div class="paragraph">
  382. <p>To activate full logging, e.g. for debugging and testing, use the <code>fine</code> level:</p>
  383. </div>
  384. <div class="listingblock">
  385. <div class="content">
  386. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">Logger.getLogger(””).setLevel(Level.FINE);</code></pre>
  387. </div>
  388. </div>
  389. </div>
  390. </div>
  391. <div class="sect1">
  392. <h2 id="advanced-error-handling"><a class="anchor" href="#advanced-error-handling"></a>Advanced Error Handling</h2>
  393. <div class="sectionbody">
  394. <div class="paragraph">
  395. <p>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.</p>
  396. </div>
  397. <div class="paragraph">
  398. <p>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.</p>
  399. </div>
  400. </div>
  401. </div>
  402. </article>
  403. <aside class="toc sidebar" data-title="Contents" data-levels="2">
  404. <div class="toc-menu"></div>
  405. </aside>
  406. </div>
  407. </main>
  408. </div>
  409. <footer class="footer">
  410. <p>Copyright 2020 jMonkeyEngine Wiki Contributors. Licensed BSD-3.</p>
  411. </footer>
  412. <script src="../../../_/js/vendor/docsearch.min.js"></script>
  413. <!-- fetched from https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.js -->
  414. <script>
  415. var search = docsearch({
  416. apiKey: 'a736b6d93de805e26ec2f49b55013fbd',
  417. indexName: 'jmonkeyengine',
  418. inputSelector: '#search-input',
  419. autocompleteOptions: { hint: false, keyboardShortcuts: ['s'] },
  420. algoliaOptions: { hitsPerPage: 10 }
  421. }).autocomplete
  422. search.on('autocomplete:closed', function () { search.autocomplete.setVal() })
  423. function focusSearchInput () { document.querySelector('#search-input').focus() }
  424. if (document.querySelector('.home-link.is-current')) window.addEventListener('load', focusSearchInput)
  425. </script>
  426. <script src="../../../_/js/site.js"></script>
  427. <script async src="../../../_/js/vendor/highlight.js"></script>
  428. </body>
  429. </html>