The need for logging in Software development
Logging and Debugging are the important steps during programming. Though logging is important post-development, especially to find statistics. It can also be used to detect the reasons for a system or a software crash. But logging is not just useful post-development, but it can also be useful during development especially for debugging purposes.
How do you debug your programs normally? I am sure that you write certain print statements or use certain debuggers like gdb. print statements is one sort of logging. But removing the print statements after debugging is a tedious task. There is one more challenge. Suppose you remove the print statements and after sometime, you encountered different sort of errors, you will have to write those statements all again.
There is a different approach for the above scenario, if you are a C/C++ developer, you would have surely heard of the pre processor statements like #ifdef, #endif
#ifdef, #endif for debugging and logging
To print a statement only during debugging, you can include those statements like the following manner
#ifdef DEBUG
printf("This is a debug statement");
#endif
Note that, this statement will only be executed if you define DEBUG anywhere in your program or you define DEBUG during compilation.
This can be done in the following manner
$gcc -D DEBUG test.c
As you can see, I define DEBUG during compilation. Not the -D option
Similarly, you can do the logging in the places where you want to include the logging
#ifdef __LOGGING__ fprintf (fp,"This is a logging statement"); #endif
fp corresponds to a file pointer pointing to some log file. Compile the above program in the following manner
$gcc -D __LOGGING__ test.c
This is the way by which you perform logging and debugging
Logging is most important part of software development. You must perform logging for
- Debugging
- Statistics
- Record keeping purposes
- Transaction tracking
- Rolling over a transaction
The last two points are related to the advanced uses of logging, where you roll back a transaction during a crash
There are packages like log4cpp, used for logging in C++ environment.
Comments: