Application logging: why is it so underused?

Probably you already have used application logging once in your life as a developer, no matter in which language you have used. So, you already know that logs help you discover a possible application failure, or even generating metrics about business of your application (most developers still don’t know this). But still people don’t know the best way to log data properly, which tools to use or even which level should be used in a certain moment.

I will show you seven tips to help you discover how to get the best from application logging (or at least try to :) )

1) Only use the .log extension

I have seen people logging their data using .txt or even other file extensions. Don’t even think about it. Since application logging does not have a specification, people try to do whatever they want, but, try to follow the common pattern and don’t invent to do something from your mind. :P (ok, only if it is something cool)

2) There are several log levels, use them.

You need to identify which levels you are going to use in each situation. To log the happy path of your application, only use “INFO”. The business exceptions and auto-recover situations should be logged as “WARN”. Uncommon exceptions should be logged as “ERROR”. There are also other levels for development such as “TRACE” and “DEBUG”, use them to log very detailed information about the flow.

3) Don’t log debug or trace levels in production

Developers seem to love wasting disk space, but, don’t do it. When you log your data in debug level in production, besides wasting space, you make your log file harder to read, so, help yourself and log just info, warn and error levels.

4) Your log needs to make sense, use key-valued data as much as possible

As much as developers love to log data in debug level, they also have some kind of affection with nonsense data. You know, the Apache kind of log. When you log your data with keys, you make your log 100% easier to read. Maybe you already know what is that UUID field in the middle of nowhere of the file, but, others developers which are also going to analyse the data do not know, so, help them to do their job. I have seen also people logging their data in json, what I found weird at first, but now I am even using it myself and enjoying it, so, if you thought of it too, do it, it really forces you use key-valued data.

5) Use well-known tools and libs

Don’t try to make your lib. Use the best lib of your language to log your data, they probably worked hard to do the best they can to improve performance and make their libs easy for you to use, so, take advantage, do not waste your time and use it.

6) Try to use aspect oriented programming to log your data

It is very hard to know when you need to log something and when you don’t. Probably you need to log every step that a flow is walking into, so, probably the best thing you need to do is to log the beginning of a method and also the end of it, so, AOP could be a good ally and log the data for you. You are probably asking yourself: “but, if my method does two different things, don’t I need to log both? AOP is not going to help me in this situation“. Well, if your method does two different things, your method is not cohesive, so, break the method up into more methods, until necessary. See, logging can help you to design your app better. :)

7) Generate metrics from your data using visual analytic tools

Logs should not be used only to find errors and problems, they can also help you to generate metrics from your business. There are several tools to help you to do that: commercial solutions like Splunk, or even open source solutions like the ELK stack (Elasticsearch + Kibana + Logstash). It is very nice to analyse your logs in a graphical way, also much easier.

Don't forget, we want less of this:

and more of this:

This is a Kibana 4 dashboard. Nice, isn't it?

I hope this post helps you choose the best decisions when you are logging your data.

Thanks. =]


Gabriel Francisco

Software Engineer at GFG, 25 years, under graduated in Computer Science and graduated in Service-oriented Software Engineering. Like playing guitar once in a while. Oh, and I'm kind of boring.

São Paulo

comments powered by Disqus