I'm wondering what kind of information should be logged to a file once an application has moved into a Production environment? Besides the logging of exceptions and errors...
Should the start and end of each method be logged? The start and end of a running service? Each time the application saves data to a database or calls an external service? I'm trying to find the balance between logging/tracing everything and only logging errors.
Logging levels are usually considered to be in order of importance: turn on "unimportant" levels in development ( trace , debug and the like), but enable only the "most important" levels ( warning , error , etc.) in production, where resources like CPU time and disk space are precious.
The TRACE log level captures all the details about the behavior of the application. It is mostly diagnostic and is more granular and finer than DEBUG log level. This log level is used in situations where you need to see what happened in your application or what happened in the third-party libraries used.
Production logs are used to evaluate fluid production and movement both inside and outside of the casing downhole. The production logging tools are small in diameter and are run through tubing for evaluation of the well as it is producing.
In a production environment I have logging set to "INFO" by default (using log4net), and at this level I log sufficient information to have a very good chance of diagnosing any errors. So what is "sufficient" information? Well, that all depends on your system. In our system I log the entry and exit points from the most important methods, including their input parameters and return values (unless this is a lot of data). I'm willing to accept an 5-10% overhead for logging (but you should measure this).
My perferred format is like this:
Method entry:
->MyMethod(1, "arg1")
Method exit:
<-MyMethod(1, "arg1") = true
The arrows mean I can easily see whether this is entry or exit. By including the arguments and return value I get the most critical data for diagnosing errors. I only ever have one return point from my methods, so I do not have to worry about multiple exit points for my logging.
By logging method entry/exit I find I do not have to log much else--if your code is properly decomposed into methods then this will document the execution flow through your application.
Do not make the mistake of not logging enough info because you are worried about the performance hit--measure this so you are happy with the overhead, but are confident that you are logging enough to diagnose faults purely based on the info that is in the log. What you do not want to have to do is switch the logging on to more detail after your customer has reported a fault, and then hope the fault occurs again.
I also use a DEBUG logging level which logs practically everything. This is only used in dev/test, or perhaps in production but only after consultation with the customer.
It really depends upon you, there are no hard & fast rules.
Couple of months back we were working on this Java application and used log4j for logging, in log4j we were able to define in code our logs as either debug, warning, error, info etc.
Our debug logging was almost on every functions start & end, every successful not successful transaction was logged as "info", "error" in exceptions were logged & likewise.
Once we moved the application in the production environment after a month or so we switched off all debug logging through .properties files without restarting the application & we were good to go.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With