Why Does the TRACE Level Exist and When to Use It Over DEBUG?

javalogging

In Log4J, Slf4J and a couple other logging frameworks in Java, you have two "developper" level for logging:

  • DEBUG
  • TRACE

I understand what DEBUG does, because the explanation is clear:

The DEBUG Level designates fine-grained informational events that are most useful to debug an application.

But the TRACE level is not very specific about its use case:

The TRACE Level designates finer-grained informational events than the DEBUG

(Source: the log4J JavaDoc)

This does not tell me how or when to use TRACE. Interestingly, this is not a severity level defined in the syslog standard. Googling for the difference between TRACE and DEBUG only seem to return "use DEBUG, oh, and there is TRACE too". I couldn't find a specific use case for the TRACE level. The best I could find was this old wiki page debating the merit of the existence of the level.

This, as an architect, raises a lot of flags and questions in my head. If a young developer asked me to add TRACE to my architecture, I would bombard him with questions:

  • What are some examples of information that should be logged with TRACE and not with DEBUG?
  • What specific problem do I solve by logging that information?
  • In those examples, what are the properties of the logged information that clearly discriminate between logging at the TRACE level rather than the DEBUG level?
  • Why must that information go through the log infrastructure?
    • What are the benefits of persisting that information in a logs journals rather than just using System.out.println ?
    • Why is it better to use log for this rather than a debugger?
  • What would be a canonical example of logging at the TRACE level?
    • What are the specific gains that have been made by logging at the TRACE level instead of DEBUG in the example?
    • Why are those gains important?
    • In reverse: What problems did I avoid by logging it at TRACE instead of DEBUG?
    • How else could I solve those problems? Why is logging at the TRACE level better than those other solutions?
  • Should TRACE level log statement be left in the production code? Why?

But given that it is present in most major framework, I am guessing it is useful for something? So… what is TRACE for, and what distinguishes it from DEBUG?

Best Answer

What are example of information that should be logged with TRACE and not with DEBUG?

If I have an algorithm that goes through a bunch of steps, trace level will print info about each of those steps at the finest level. Things like the literal inputs and outputs of every step.

In general, trace will include all debug (just like debug includes all warnings and errors).

What specific problem do I solve by logging that information?

You need to debug something that outputs way too much data to log outside of a specific build when you're targeting that particular thing and do not care about errors or other logging info (since the volume of trace info will obscure them). In some loggers, you will turn a certain module up to trace level only.

In those examples, what are the properties of the logged information that clearly discriminate between logging at the TRACE level rather than the DEBUG level?

In general, trace level logging cannot be on for sustained periods because it degrades the performance of the application greatly, and/or creates an abundance of log data that is unsustainable due to disk/bandwidth constraints.

Debug level logging can usually be on for a longer period without making the app unusable.

Why does that information must go through the log infrastructure?

It doesn't have to. Some frameworks have a separate tracelogger.

Usually it ends up in the logs since traceloggers and normal loggers have similar needs with regards to writing to disk/network, error handling, log rotation, etc.

Why is it better to use log for this rather than a debugger?

Because the debugger might not be able to attach to the problem machine. You might not know enough to know where to even set breakpoints, or to step through code. You might not be able to reliably reproduce the error in a debugger, so use the logs to catch it "if it happens".

But they're just names. Like any other label, they're just names people put on things, and will usually mean different things to different people. And the label itself is less important than the meaty bits that the label refers to.

Related Topic