Why to log to stdout?
I have three nephews, and I thought I need to be a caring aunt for them, so I thought I would buy a book called Hello Ruby: Adventures in Coding for Christmas. I read a few pages, and I loved one of the characters’ favorite sentences: Why? My first thought was: this should be normal.
Besides, I realized that some of my colleagues do not understand why logs should be sent to the stdout, they somehow identified it as an antipattern. So, I decided to collect the advantages of this best practice.
Pet or cattle?
Nowadays microservices architectures consist of docker containers, and Docker containers should be ephemeral, which means they should be disposable at any time since you want to have cattle, not pet servers.
Cattle means that if one of your services is unhealthy, you don’t want to fix it (or pet it), you want to be redeployed automagically, and because of the immutability of docker containers, you will get the same result every time. So, if it works one time, it should work every time the same way.
If you store your logs in a container, how can you get the logs, if a container is killed?
Fun fact: Docker has its built-in logging drivers. ;)
Centralization is fun!
You can use a central logging solution. Regardless it’s Splunk, Graylog, or your implementation in AWS, it’s an awesome concept. Fast, searchable, and you don’t have to open more than 10 terminal windows, log in to the server/container, and get the logs, and search in them by hand. Because, of course, you want your system to be scalable.
Also, sometimes you have to get data from a pipeline of services. One after the other. You can reach your data much faster in a centralized way.
So, centralized logging is practical. And the easiest way to get your logs is by streaming your logs.
One of my favorite collections of best practices is the 12-factor app. If you haven’t heard of it, you should read it. The XIth point of it is about logging and it says that logs should be treated as event streams because it gives more flexibility for investigating problems.
All in all,
Asking why is fun. Or you can think about other solutions from the common ones so you can discover many wrong solutions. Once Edison said:
“I have not failed 10,000 times. I have not failed once. I have succeeded in proving that those 10,000 ways will not work. When I have eliminated the ways that will not work, I will find the way that will work.”