Search code examples
loggingarchitecture

Is it worth obtain data from the database just so the log can show more information?


I'm in a situation where I need to add a log to my code to facilitate debugging if a problem arises later. For example, here is a method:

SomeMethod(Request request){

   ....

   // I need add log here
    ...

}

The Request object contains:

Request
{
   string typeId,
   string userId
}

I was considering outputting the corresponding type name and user name from the request in the log, but there is no direct source in SomeMethod where I can get this information, so I had to request the database. The question I would like to ask is:

  1. is it necessary to output the name instead of just the id?
  2. is it worth getting the information from the database specifically for the log only?

I think it shouldn't be unnecessary to get specific data from the database just for the log; and the name may change after the log, making it impossible to correspond, so logging the ID is sufficient. May I ask your opinion?


Solution

  • I think it shouldn't be unnecessary to get specific data from the database just for the log; and the name may change after the log, making it impossible to correspond, so logging the ID is sufficient.

    I take it you meant "it shouldn't be necessary".

    There is a valid argument to the effect that information in logs should be as self-contained as possible (just the name is sufficient for immutable data, e.g. a fixed table of types, both the ID and the name should go with mutable data): 1) for readability/usability by the developer/technician (for example a type ID is more opaque than a type name), but also 2) because data indeed can change (mutable data), and the log should rather reflect the situation at the moment of logging!

    Which is usually fine as long as performing those queries is relatively cheap (and/or if there is some caching behind the scenes), especially if the level of logging can be controlled such that full logging with detailed queries is only needed in debug/maintenance mode, not in normal operations, and anyway care is taken to log only what is strictly relevant in the specific logging context.