Log is a critical part of an application. It serves as an eye to the programmer on how the application is working while debugging. Especially for applications running on production environment, if the application encounters problem and the problem cannot be reproduced on other environments, log will be extremely useful.
While log is essential, but developers have to log smartly. Because if don't put log smartly, you may not get what you want while debugging or you may get too many redundant logs which eat up the disk space and degrade the performance of the system.
So what and what to log in an application while debugging? Here we summarize some which we think may be useful to you.
What to log
- Log application information. This information includes the application name, version, build date etc. This is to ensure we are debugging on a correct version of application.
- Log configuration information. If there are configuration options used, for example, which mode is the application running. These information will give an overview of how the application is configured and gives the direction for debugging.
- Log path information. When the application tries to read some external resource(including file, socket etc), the path to the resource should be logged so that the correct resource is ensured being read.
- Log branch information. When there are control flows in the program(if else etc), please log which branch the program is entering.
- Log exception. In cases where some exception will be thrown and they are encapsulated in some standard exceptions, please log the original exception information. This usually helps reveal the root cause of the problem. DO NOT SWALLOW EXCEPTION SILENTLY!
- Log pre and post data information. When processing external data, for example, encoding/decoding data, encrypting/decrypting data, please log the input data before processing and the output data after processing. This helps check whether the data processing logic/algorithm is correct.
- Log thread information. In multithreading applications, log the thread information so that you can know what each thread is doing.
- Log state information. When writing applications which involve many state changes(For example, SSL application), please remember to log the state information after each state change. This helps understand where the program is now when something unexpected happens.
What not to log
- Do not log sensitive information. Be careful when planning to log sensitive information such as password, keys. Especially when developing applications for other companies.
- Do not alter information while logging. Pay attention to the information you are logging, do not alter the status of the information. These information may be variables which are to be referenced in the normal program execution. Sometimes, the variable is changed when logging and it then affects the end result of the program execution. So be careful.
- Do not log too much in loops. When writing loops(for example for loop, while loop), please log only the necessary information if possible. Because if log too much in loops, it's easy that the disk will be jammed and you may not get what you want as well.
- Do not log repeat information. Sometimes you may have logged some parameters before passing them to a method, then you may forget about it and add the same log info in the method call. In this case, some redundant information is logged. Please avoid it. Create a rule on when the parameters should be logged.
- Do not log ambiguous information. When writing log, please log the complete information, do not log ambiguous information which needs the developer to guess what is going on.
- Do not log big data object. If you expect the application will process big data object(MBs, GBs), evaluate carefully before logging it. Is it possible just to log part of the data object which is necessary?
A good log will ease your daily work. Sometimes an urgent issue can be located and resolved quickly with the proper log information.