Shell script is powerful, quick, easy to write and gets your work done. But the real problem is when you want to figure out the failure point in your script. With any compiled language, its easy to identify code flaws / exceptions / errors using good amount of logging statements. However, shell script is an interpreted (not compiled) language and the only way to debug it, is by adding echo statements to your scripts and then either display it on console or re-direct it to some log file.

Lets look at richness of logging statements in a compiled language like JAVA or C++. To begin with, System.out.println outputs to standard output, which
typically is a console window. The output from Log4j can go to the
console, but it can also go to an email server, a database table, a log file, or various other destinations.

Another benefit of Log4j is that we can set different levels of logging based on the depth of analysis needed. The levels are hierarchical and are as follows: TRACE, DEBUG, INFO, WARN, ERROR, and FATAL. Shown below are sample logs using a log4j library:

2016-02-25 20:52:39 [DEBUG] This is debug
2016-02-25 20:52:39 [INFO] This is info
2016-02-25 20:52:39 [WARN] This is warn
2016-02-25 20:52:39 [ERROR] This is error
2016-02-25 20:52:39 [FATAL] This is fatal

Similar level of logging mechanism for shell scripts is difficult to achieve since we can only write echo "some string" type of statements. Well, I mentioned it previously also "shell scripts are powerful and gets your work done". Let's try to simulate a similar behaviour for logging in shell scripts.

Expected format:
TimeStamp LogLevel Message

We need to create three common functions with names INFO, DEBUG, ERROR as part of the logger.sh helper script. We also need a way to track which function of which script is being invoked, just like the finest level of debugging in JAVA. Below is the helper script: