35
(Conditional) Logging Considered Harmful JAX London 2014

Conditional Logging Considered Harmful - Sean Reilly

Embed Size (px)

Citation preview

Page 1: Conditional Logging Considered Harmful - Sean Reilly

(Conditional) Logging Considered Harmful

JAX London 2014

Page 2: Conditional Logging Considered Harmful - Sean Reilly

About MeSean Reilly

@seanjreilly

Page 3: Conditional Logging Considered Harmful - Sean Reilly

Logging Is Not Harmful

In fact, it’s often useful.

Conditional logging is harmful.

Page 4: Conditional Logging Considered Harmful - Sean Reilly

What I Mean By “Conditional”

• Many loggers, organised by class name• Many log levels:

• SEVERE, ERROR, WARN, INFO, DEBUG, TRACE

• SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST

• ERROR, WARN, INFO, DEBUG, TRACE• Message only logged if log level is active for a logger

Page 5: Conditional Logging Considered Harmful - Sean Reilly

Conditional Logging Frameworks

• log4j• java.util.logging• Logback• slf4j• commons logging

Page 6: Conditional Logging Considered Harmful - Sean Reilly

Why Developers Log This Way

• “We can log everything in development and INFO in production”

• “When something unexpected happens in production, we can increase the log level dynamically and get the extra information we need”

• “It helps us debug while developing”

Page 7: Conditional Logging Considered Harmful - Sean Reilly

The Real Reason

What other options are there?

Page 8: Conditional Logging Considered Harmful - Sean Reilly

Why Did We Decide To Use Something Else?

Page 9: Conditional Logging Considered Harmful - Sean Reilly

The Real Reason Why

Page 10: Conditional Logging Considered Harmful - Sean Reilly

A Poor Fit For Agile Development

• Difficult to test• Bad OO practices• Not friendly with dependency injection• Not Spring/Guice/IoC Container compatible• Custom configuration files• Not refactor-friendly• Produces important (maybe) untested code

Page 11: Conditional Logging Considered Harmful - Sean Reilly

Who Should Logging Be For?

Operations!

Page 12: Conditional Logging Considered Harmful - Sean Reilly

What About Developers?

Developers should run unit tests in a

debugger

Page 13: Conditional Logging Considered Harmful - Sean Reilly

What To Change?• Not just a change in libraries• Not just a change in pattern• Change the process!

Log for operations, not developers

Page 14: Conditional Logging Considered Harmful - Sean Reilly

The New Process

Page 15: Conditional Logging Considered Harmful - Sean Reilly

What Most Teams Do Now• Developers add logging while working on other stories

• Usually without much guidance

• Hand off to operations at the end• Operations figures out how to monitor• This isn’t tested• This usually isn’t much of a feedback loop

• When there is, it’s probably informal

Page 16: Conditional Logging Considered Harmful - Sean Reilly

A Better Process• Stories for logging in the backlog

• The Operations team is the customer

• Collaborate between ops and dev

Page 17: Conditional Logging Considered Harmful - Sean Reilly

A Sample Story“As operations, I want a log entry to be written when the

application starts, so that I can determine how often this happens over a period of

time”

Page 18: Conditional Logging Considered Harmful - Sean Reilly

Operations Deserves

• To know exactly what messages can occur

• What each message means• Exactly what they should do

Page 19: Conditional Logging Considered Harmful - Sean Reilly

Operations Deserves

Sometimes this is “something completely

unexpected has happened, and you should

call the developers”.

Page 20: Conditional Logging Considered Harmful - Sean Reilly

The New Pattern

Page 21: Conditional Logging Considered Harmful - Sean Reilly

Enum Based Logging

Page 22: Conditional Logging Considered Harmful - Sean Reilly

Enum Based Logging

• An enum of all log messages

Page 23: Conditional Logging Considered Harmful - Sean Reilly

Enum Based Logging

• An enum of all log messages• Each enum value has a unique code

Page 24: Conditional Logging Considered Harmful - Sean Reilly

Enum Based Logging• An enum of all log messages• Each enum value has a unique code

• Each enum value has a format string

Page 25: Conditional Logging Considered Harmful - Sean Reilly

Enum Based Logging• An enum of all log messages• Each enum value has a unique code• Each enum value has a format string• To log: provide an enum value and format string arguments

Page 26: Conditional Logging Considered Harmful - Sean Reilly

The Output

2014-­‐04-­‐30T17:46:49Z,-­‐,GDS-­‐0000,Published  42  records  2014-­‐04-­‐30T17:48:18Z,-­‐,GDS-­‐0000,Published  64  records  2014-­‐04-­‐30T17:52:45Z,-­‐,GDS-­‐0006,Config  file  foo.conf  not  found

Page 27: Conditional Logging Considered Harmful - Sean Reilly

Code Sample

Page 28: Conditional Logging Considered Harmful - Sean Reilly

Open Source

Page 29: Conditional Logging Considered Harmful - Sean Reilly

The Library• Open source (Apache2)• Opinionated• Simple output, easy to read, easy to parse

• Small, simple, no transitive dependencies• Java 8 required

Page 30: Conditional Logging Considered Harmful - Sean Reilly

Testability Features• Designed for testability• Special test double• Mock framework agnostic• Contract tests for your enums• OpsLogger instances always use injection

Page 31: Conditional Logging Considered Harmful - Sean Reilly

Unique Features• Can automatically generate documentation

• Logrotate friendly• Easy to parse with logstash• Special stack trace handling

Page 32: Conditional Logging Considered Harmful - Sean Reilly

Project Status• Release Candidate• Production Quality Code, properly tested• Good feature set• Needs documentation• Use in a serious production project before version 1.0

Page 33: Conditional Logging Considered Harmful - Sean Reilly

Gradle Dependencies

repositories  {  

  jcenter()  

}  

dependencies  {  

  compile  “com.equalexperts:opslogger:0.1.0-­‐rc1"  

  testCompile  “com.equalexperts:opslogger-­‐support:0.1.0-­‐rc1"  

}

Page 34: Conditional Logging Considered Harmful - Sean Reilly

Maven Dependencies<dependency>  

  <groupId>com.equalexperts</groupId>  

  <artifactId>opslogger</artifactId>  

  <version>0.1.0-­‐rc1</version>  

</dependency>  

<dependency>  

  <groupId>com.equalexperts</groupId>  

  <artifactId>opslogger-­‐support</artifactId>  

  <version>0.1.0-­‐rc1</version>  

  <scope>test</scope>  

</dependency>

Page 35: Conditional Logging Considered Harmful - Sean Reilly

Thanks!