Logs
-
Upload
hanoh-aloni -
Category
Software
-
view
31 -
download
0
Transcript of Logs
![Page 1: Logs](https://reader035.fdocuments.us/reader035/viewer/2022081605/58ee7afd1a28abe0198b45b3/html5/thumbnails/1.jpg)
Logs
![Page 2: Logs](https://reader035.fdocuments.us/reader035/viewer/2022081605/58ee7afd1a28abe0198b45b3/html5/thumbnails/2.jpg)
Objective
• all activities that affect user state or balances are formally trackedAuditable
• it’s possible to determine where an activity occurs in all tiers of the application
Traceable
• logs cannot be overwritten or tampered with by local or remote users
High integrity
![Page 3: Logs](https://reader035.fdocuments.us/reader035/viewer/2022081605/58ee7afd1a28abe0198b45b3/html5/thumbnails/3.jpg)
Message Priorities/Levels
• Severe errors that cause premature termination. fatal • Other runtime errors or unexpected conditions. error • are undesirable or unexpected, but not necessarily
"wrong". Warn• Interesting runtime events (startup/shutdown). Info• detailed information on the flow through the system.Debug• more detailed information. Trace
![Page 4: Logs](https://reader035.fdocuments.us/reader035/viewer/2022081605/58ee7afd1a28abe0198b45b3/html5/thumbnails/4.jpg)
What to log?
Log all the thingsWork Smarter, not Harder
![Page 5: Logs](https://reader035.fdocuments.us/reader035/viewer/2022081605/58ee7afd1a28abe0198b45b3/html5/thumbnails/5.jpg)
Default Message Priority/Level
By default the message priority should be no lower than info.
That is, by default debug message should not be seen in the logs.
![Page 6: Logs](https://reader035.fdocuments.us/reader035/viewer/2022081605/58ee7afd1a28abe0198b45b3/html5/thumbnails/6.jpg)
Default production log level
WARNING
![Page 7: Logs](https://reader035.fdocuments.us/reader035/viewer/2022081605/58ee7afd1a28abe0198b45b3/html5/thumbnails/7.jpg)
Where to define logger layout?
• private static Logger log = Logger.getLogger( MyClass.class )– log.error()– log.warn()– log.info()– log.debug()
• "%r [%t] %-5p %c - %m%n" – 176 [main] INFO org.foo.Bar - Located nearest gas
station.
![Page 8: Logs](https://reader035.fdocuments.us/reader035/viewer/2022081605/58ee7afd1a28abe0198b45b3/html5/thumbnails/8.jpg)
What format to use?
• A SINGLE LINE• Timestamps with timezone, to the
millisecond or nanosecond• Class name + line number• Meaningful context
![Page 9: Logs](https://reader035.fdocuments.us/reader035/viewer/2022081605/58ee7afd1a28abe0198b45b3/html5/thumbnails/9.jpg)
Best practices for app
• Do not allow exceptions to go unhandled/reach the browser
• Display custom error pages to users with an email link for feedback
• Do not enable “Robust Exception Information” in production.
• Rotate log files to keep them current
![Page 10: Logs](https://reader035.fdocuments.us/reader035/viewer/2022081605/58ee7afd1a28abe0198b45b3/html5/thumbnails/10.jpg)
Use parameterized logging
• LOG.debug("Method called with arg " + arg);• BAD: string varies with argument
![Page 11: Logs](https://reader035.fdocuments.us/reader035/viewer/2022081605/58ee7afd1a28abe0198b45b3/html5/thumbnails/11.jpg)
Use parameterized logging
• if (LOG.isDebugEnabled()) { LOG.debug("Method called with arg {}", arg);
• BAD: code clutter
![Page 12: Logs](https://reader035.fdocuments.us/reader035/viewer/2022081605/58ee7afd1a28abe0198b45b3/html5/thumbnails/12.jpg)
Use parameterized logging
• LOG.debug("arg is {}", arg);• BAD: wrong level of language, this would be
okay on TRACE
![Page 13: Logs](https://reader035.fdocuments.us/reader035/viewer/2022081605/58ee7afd1a28abe0198b45b3/html5/thumbnails/13.jpg)
Use parameterized logging
• LOG.debug("Method called with arg {}", arg);• GOOD: string literal, no dynamic objects
![Page 14: Logs](https://reader035.fdocuments.us/reader035/viewer/2022081605/58ee7afd1a28abe0198b45b3/html5/thumbnails/14.jpg)
Exception example
• catch (SomeException ex) { LOG.warn("Failed to do something with {}, continuing", arg, ex); }
• GOOD: note how there is no "{}" for ex
![Page 15: Logs](https://reader035.fdocuments.us/reader035/viewer/2022081605/58ee7afd1a28abe0198b45b3/html5/thumbnails/15.jpg)
Provide useful event context
• LOG.debug(arg.toString());• VERY BAD: – no context provided– non-constant message string– assumes useful toString()
![Page 16: Logs](https://reader035.fdocuments.us/reader035/viewer/2022081605/58ee7afd1a28abe0198b45b3/html5/thumbnails/16.jpg)
Provide useful event context
• LOG.debug("{}", arg);• VERY BAD– no context provided
![Page 17: Logs](https://reader035.fdocuments.us/reader035/viewer/2022081605/58ee7afd1a28abe0198b45b3/html5/thumbnails/17.jpg)
Provide useful event context
• try { doSomething(arg); ... } catch (SomeException ex) { }
• COMPLETELY BAD– silently ignoring errors!!!
![Page 18: Logs](https://reader035.fdocuments.us/reader035/viewer/2022081605/58ee7afd1a28abe0198b45b3/html5/thumbnails/18.jpg)
Provide useful event context
• try { doSomething(arg);} catch (SomeException ex) { LOG.warn(ex.getMessage()); }
• EXTREMELY BAD– message is not constant– no context is provided– ex.getCause() is lost– call stack is lost
![Page 19: Logs](https://reader035.fdocuments.us/reader035/viewer/2022081605/58ee7afd1a28abe0198b45b3/html5/thumbnails/19.jpg)
Provide useful event context
• try { doSomething(arg); ... } catch (SomeException ex) { LOG.warn("Failed to do something with {}, ignoring it", arg, ex); }
• GOOD– string literal– we explain what we tried to do– we pass along information we have about the failure – we explain that we recovered from the failure
![Page 20: Logs](https://reader035.fdocuments.us/reader035/viewer/2022081605/58ee7afd1a28abe0198b45b3/html5/thumbnails/20.jpg)
Provide useful event context• try { doSomething(arg); ... }
catch (SomeException ex) { LOG.error("Failed to do something with {}", arg, ex); throw new RuntimeException("Failed to do something", ex); }
• GOOD– string literal– we explain what we tried to do– we pass along information we have about the failure – we escalate the failure to our caller– we also 'chain' the exception so it is not lost and can be correlated
![Page 21: Logs](https://reader035.fdocuments.us/reader035/viewer/2022081605/58ee7afd1a28abe0198b45b3/html5/thumbnails/21.jpg)
Anything else?
• Separate files for different areas of interest• Always Log To Local Disk
![Page 22: Logs](https://reader035.fdocuments.us/reader035/viewer/2022081605/58ee7afd1a28abe0198b45b3/html5/thumbnails/22.jpg)
• http://juliusdavies.ca/logging.html