How I decide on log levels for applications

I have been thinking a lot in deciding when to use which log level at a particular point in the application. After a lot of trial and error, I have now settled on a couple of points by which I am going to log levels. They are not in any way to be considered as absolute but rather my current position which may change in the near future.

TRACE: I use this level to dump necessary data(in JSON format or call toString()) for debugging. Invoke this level only after checking if the trace is enabled since the process of converting the data to string can be expensive. This level comes in handy when all other options of debugging an issue is exhausted.

DEBUG: Imagine you have entered a DAO layer. You have to perform a search for the post in the cache first. If not present in the cache we query the database, get the data and put the same in cache. Each of the steps will logged as debug level.

INFO: I am in the service layer and I am going to call the DAO layer. This is one of the operations that is an essential part of the request so I will log an INFO level message before I call the DAO layer. I am not entirely convinced If I should log an INFO message once the DAO layer operation is complete. Thinking about it.

The main difference I see between INFO and DEBUG levels is that I use INFO levels before I enter a module that is expensive and on whose output, the response that I build is completely dependent on.

WARN: I log warn messages when I receive an exception but I am going to continue with the request. Exception message will be included in the log but not the stack-trace. I am not going to get anything out of the stack-trace. Let’s say the request needs some social media information but when I try to connect to twitter, I get an exception. This is not a reason to stop the request so I log a warn message and proceed with the request.

ERROR: I log error messages when it makes no sense to continue with the request. If I don’t get data from the db, I will log an error message. I will log the stack-trace only at the top level though. Let’s say the request comes from the controller to the service layer and then to the DAO layer. DAO layer throws an exception saying that no data is available for the request parameter.

In both cases, stack-trace will be logged only at the top level after which the only work of the thread is to respond back to the client request.