What to log…
When you manage & run your applications & systems, logging is an essential and critical component you must enable.
So, the question in logging should be ‘what to log’
For example, those below should be logged in your logging system.
- Requests
- Audit Trail
- Availability
- Threats
- Events
(ref: https://towardsdatascience.com/the-5-most-important-logs-an-application-should-write-53aea35c740f)
Another practice can be introduced below…
https://onloupe.com/solutions/what-should-i-log-in-my-application/
- Application Session start/stop, user Session start/stop, Unhandled Exceptions, Handled Exceptions, Process Entrance and Exit, Significant User Actions. (Button actions, Navigating to a new context, Any message box / modal prompt displayed, Expensive operations), Display Help requests, Cancelled Actions, Log Thread start/stop, Asynchronous request start/stop.
However, you can’t log every you need, as ‘logging is a cost’. You should focus on what/how much. In other words, the log should not be too much nor too little, which is called Goldilogs. Owasp.com also defines poor logging practice. https://owasp.org/www-community/vulnerabilities/Poor_Logging_Practice.
Wastes…:
- Computation resources to log them
- Storage for the logs
- Resource & time to find the event
In “Code That Fits in your head” by Mark Seeman, the log should
- Having repeatability (able to reproduce execution)
- Log only impure actions, no more.
(Ref: https://blog.ploeh.dk/2020/03/23/repeatable-execution/)
Must consider what NOT to log as well
You should also think about how you avoid unnecessary and useless logs, more seriously, logs that you must not log (e.g., PII data).
- Don’t log deterministic result
Log.Debug($”{x} and {y});
int sum = x + y;
Log.Debug($”Sum is {z});
Do you really think you should log Log.Debug($”Sum is {z}) ? do you want to waste your resources to store that meaningless data? - Avoid logging in tight loops
- The Sensitive information: Logs is another database but with less authentication, authorization check. We put incredible attention to protecting your database and its access whereas almost all can access and see the system & application logs.
Lastly, if the development team spends 100 hours developing their features, they need to spend another 100 hours thinking about their code quality, such as readable, loggable, reusable, testable, etc.
Logging is more important than you think.