Logging
Podcast: Play in new window | Download (44.6MB) | Embed
Subscribe: Apple Podcasts | Spotify | Email | RSS | More
“I’ve discovered a lot of JavaScript Promises issues because if you throw in a debugger it works.”
Logging provides routine diagnostic information when a debugger is not running such as in production. It is used to provide runtime proof that the app is still running (not in a zombie state), an audit trail or other forensic data, and troubleshoot rare bugs that can’t be reproduced in development.
Episode Breakdown
14:57 What A Logging Framework Should Do
“The point of it isn’t just I’m dumping stuff to a text file, because you did that in development 101.”
Asynchronous I/O to write logs. Your logging framework should not impede your main thread. It should also gracefully handle and recover from error conditions. You don’t want a logging error to bring down the application, and you don’t want to have to restart the application to get it logging again. A framework should allow runtime reconfiguration of logging levels based on severity and area of the code.
“Arbitrary always means when you are asleep.”
In addition a good framework will allow log rotation. In other words, only keep x days of logs. The system needs to remove old ones automatically or support archiving them so that they don’t fill up the drive. Logging to multiple locations, either concurrently, or as a chain of fallbacks. You want to be able to configure failover locations or writes to multiple locations in case something doesn’t work.
23:00 What A Logging Framework Shouldn’t Do
“Logging should never interfere with the runtime of the app.”
A logging framework should not require a lot of effort for a first pass. Your logging framework should be able to safely hook into the guts of an existing application, without requiring a lot of work to support it for the simplest use cases. Your logging framework MUST NOT EVER exclusively lock the log file so that other apps can’t read it. Otherwise you’ll have to turn off your app to read the logs or even copy them elsewhere in many cases.
26:53 Do Not Write Your Own Framework
Everyone tries to do this. On the outset it appears to be easy and not a significant amount of effort. A logging framework of your own does not add value to your application. It’s also quite difficulty to provide the flexibility that most mature applications will require on your own.
“I’ve thought about it and I’ve looked at it and thought that’s just a stupid idea.”
Most developers using your language will have experience with one or more of the most common logging frameworks out there. They won’t have experience with yours. It’s really easy to screw up. You have to think about weird edge cases, like how to make absolutely certain that you flush your logging buffer before the application terminates, even if the application does so abruptly.
34:28 Choosing the right severity level.
“You need to log that as what severity level it is in regards to the continual functioning of the application.”
You shouldn’t be marking everything as “Verbose” (or equivalent) any more than you should be marking everything as “Fatal” or equivalent.
37:17 Choosing a good log rotation strategy.
Ideally, you’d be able to look in no more than a couple of files to find the log entries for a particular day and would be able to search within to find the entry you need quickly.
Be cautious of using timestamps, especially if the server is on a different timezone than the people who are kicking and screaming about things being wrong.
44:56 Stack Trace
Configure the logs to give you a stack trace where applicable. Once you find a problem in the logs, you still need to know where it is in the code. Be sure and have a utility that can watch a log file as it is being written and keep the display up to date.
IoTease: Project
Life Size BB-8 Droid
This is a phone controlled BB-8 replica that you can build at home. The tutorial uses household materials to build the body of the droid and an Arduino Uno to control it. He also includes videos in the tutorial. It’s a very inventive idea.
Tricks of the Trade
Don’t just log in your code. Log in your life. Keep a diagnostic trail around yourself that you can look back on. You’ll be surprised at the personal insight that this will give you.
Editor’s Notes:
Will and BJ were both getting over colds when recording this episode. Most of the coughs and sniffles have been removed but please ignore the ones that could not be cut from the audio.