Batch Logging Done Right

I have come across too many batch scripts that write to log files with little response to the user, if any at all. Here is a little snippet that attempts at doing it right, and with little overhead for programmers. Hopefully it is self-explanatory and well commented.



The gist of it all is in having a single function that can redirect output to the console, the log or both. The datetime stamp is just a clue about how long it takes to run the script, or a particular process within the script.

Obviously plain ECHO commands (such as the script's banner) do not make it into the log.

Here is the console output:

===============================================================
HJR Log Test Utility, Version 1.0, Fri Sep 14 13:53:16 CDT 2012
===============================================================

2012-09-14 14:49:17 logtest.bat started.
2012-09-14 14:49:19 logtest.bat finished.
2012-09-14 14:49:19 Done.

And here is the output file (test.log) contents:

2012-09-14 14:49:17 logtest.bat started. 
2012-09-14 14:49:19 Process completed succesfully. 
2012-09-14 14:49:19 logtest.bat finished.
Hector [.j.] Rivas
Hector [.j.] Rivas
Rowlett, TX | 214.789.1733 | hector.j.rivas@hotmail.com