In computing, a log is an activity record generated by a system event. Logs are highly useful for monitoring, traceability, analyzing, verification, and to measure all activities related to the system.
There is a misconception that debugging is the same as logging. It is not. Debug messages are usually cryptical and only make sense for those who wrote them. Logs are not just for developers but for support teams, auditors, system administrators, basically for everyone that interacts with the system.
What to log depends on the system’s type and the type of events that wants to be analyzed. The most common activities are:
- Authentications, authorizations, and accesses.
- Application’s changes or information changes.
- Invalid inputs.
- Resources issues.
- Availability issues.
A log record should always answer the 5W’s, which are:
- When the event happened (Timestamp).
- Where it happened (Class, component, application).
- Who triggered the event (User or service).
- What action the event performed (Action).
- Why the event was triggered (Reason).
Optional information can be added to the log record, such as:
- Operation result (Was a success, or a failure)
- Priority (Severity, rank, level).
Here is a list of recommendations to follow when you are planning the log:
Use a standard format for the system’s log (And if possible, spread the same log format across the company).
Use the same wording for the same context. It is easy to read and analyze logs when they use the same words to express meaning.
Do not (I repeat, do not) include any confidential information in your log, passwords, tokens, or secret keys, are out. You might ire your security engineer if you do.
Avoid subjective/interpretative messages, anyone familiar with the system should be able to interpret what exactly happened.
Non-human readable formats (like XML, blobs, stack-traces) are considered bad practice if a context is not provided. Always try to give them context.
And last but not least, make logging part of your coding workflow. You will be thankful to have logs when your system goes down.