conditional logging considered harmful - sean reilly

Post on 31-Jul-2015

87 Views

Category:

Presentations & Public Speaking

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

(Conditional) Logging Considered Harmful

JAX London 2014

About MeSean Reilly

@seanjreilly

Logging Is Not Harmful

In fact, it’s often useful.

Conditional logging is harmful.

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

Conditional Logging Frameworks

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

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”

The Real Reason

What other options are there?

Why Did We Decide To Use Something Else?

The Real Reason Why

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

Who Should Logging Be For?

Operations!

What About Developers?

Developers should run unit tests in a

debugger

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

Log for operations, not developers

The New Process

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

A Better Process• Stories for logging in the backlog

• The Operations team is the customer

• Collaborate between ops and dev

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”

Operations Deserves

• To know exactly what messages can occur

• What each message means• Exactly what they should do

Operations Deserves

Sometimes this is “something completely

unexpected has happened, and you should

call the developers”.

The New Pattern

Enum Based Logging

Enum Based Logging

• An enum of all log messages

Enum Based Logging

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

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

• Each enum value has a format string

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

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

Code Sample

Open Source

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

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

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

Unique Features• Can automatically generate documentation

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

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

Gradle Dependencies

repositories  {  

  jcenter()  

}  

dependencies  {  

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

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

}

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>

Thanks!

top related