conditional logging considered harmful - sean reilly
TRANSCRIPT
![Page 1: Conditional Logging Considered Harmful - Sean Reilly](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/1.jpg)
(Conditional) Logging Considered Harmful
JAX London 2014
![Page 2: Conditional Logging Considered Harmful - Sean Reilly](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/2.jpg)
About MeSean Reilly
@seanjreilly
![Page 3: Conditional Logging Considered Harmful - Sean Reilly](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/3.jpg)
Logging Is Not Harmful
In fact, it’s often useful.
Conditional logging is harmful.
![Page 4: Conditional Logging Considered Harmful - Sean Reilly](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/4.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/5.jpg)
Conditional Logging Frameworks
• log4j• java.util.logging• Logback• slf4j• commons logging
![Page 6: Conditional Logging Considered Harmful - Sean Reilly](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/6.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/7.jpg)
The Real Reason
What other options are there?
![Page 8: Conditional Logging Considered Harmful - Sean Reilly](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/8.jpg)
Why Did We Decide To Use Something Else?
![Page 9: Conditional Logging Considered Harmful - Sean Reilly](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/9.jpg)
The Real Reason Why
![Page 10: Conditional Logging Considered Harmful - Sean Reilly](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/10.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/11.jpg)
Who Should Logging Be For?
Operations!
![Page 12: Conditional Logging Considered Harmful - Sean Reilly](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/12.jpg)
What About Developers?
Developers should run unit tests in a
debugger
![Page 13: Conditional Logging Considered Harmful - Sean Reilly](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/13.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/14.jpg)
The New Process
![Page 15: Conditional Logging Considered Harmful - Sean Reilly](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/15.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/16.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/17.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/18.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/19.jpg)
Operations Deserves
Sometimes this is “something completely
unexpected has happened, and you should
call the developers”.
![Page 20: Conditional Logging Considered Harmful - Sean Reilly](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/20.jpg)
The New Pattern
![Page 21: Conditional Logging Considered Harmful - Sean Reilly](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/21.jpg)
Enum Based Logging
![Page 22: Conditional Logging Considered Harmful - Sean Reilly](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/22.jpg)
Enum Based Logging
• An enum of all log messages
![Page 23: Conditional Logging Considered Harmful - Sean Reilly](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/23.jpg)
Enum Based Logging
• An enum of all log messages• Each enum value has a unique code
![Page 24: Conditional Logging Considered Harmful - Sean Reilly](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/24.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/25.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/26.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/27.jpg)
Code Sample
![Page 28: Conditional Logging Considered Harmful - Sean Reilly](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/28.jpg)
Open Source
![Page 29: Conditional Logging Considered Harmful - Sean Reilly](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/29.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/30.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/31.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/32.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/33.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/34.jpg)
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](https://reader033.vdocuments.site/reader033/viewer/2022042701/55bab9cbbb61ebea718b4791/html5/thumbnails/35.jpg)
Thanks!