Logs

22
Logs

Transcript of Logs

Page 1: Logs

Logs

Page 2: Logs

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

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

What to log?

Log all the thingsWork Smarter, not Harder

Page 5: Logs

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

Default production log level

WARNING

Page 7: Logs

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

What format to use?

• A SINGLE LINE• Timestamps with timezone, to the

millisecond or nanosecond• Class name + line number• Meaningful context

Page 9: Logs

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

Use parameterized logging

• LOG.debug("Method called with arg " + arg);• BAD: string varies with argument

Page 11: Logs

Use parameterized logging

• if (LOG.isDebugEnabled()) { LOG.debug("Method called with arg {}", arg);

• BAD: code clutter

Page 12: Logs

Use parameterized logging

• LOG.debug("arg is {}", arg);• BAD: wrong level of language, this would be

okay on TRACE

Page 13: Logs

Use parameterized logging

• LOG.debug("Method called with arg {}", arg);• GOOD: string literal, no dynamic objects

Page 14: Logs

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

Provide useful event context

• LOG.debug(arg.toString());• VERY BAD: – no context provided– non-constant message string– assumes useful toString()

Page 16: Logs

Provide useful event context

• LOG.debug("{}", arg);• VERY BAD– no context provided

Page 17: Logs

Provide useful event context

• try { doSomething(arg); ... } catch (SomeException ex) { }

• COMPLETELY BAD– silently ignoring errors!!!

Page 18: Logs

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

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

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

Anything else?

• Separate files for different areas of interest• Always Log To Local Disk

Page 22: Logs

• http://juliusdavies.ca/logging.html