Created
June 6, 2016 21:12
-
-
Save atheiman/e4de873c08751ff9b89b8de2c6ce9c69 to your computer and use it in GitHub Desktop.
simple shell script logging utility - logging.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Prints all arguments passed to the function with a timestamp prepended. `LOG_DATE_STRING` can be | |
| # overridden by exporting the var. Prepend to the log using `LOG_LEVEL`. | |
| log() { | |
| [[ -z "$LOG_LEVEL" ]] && log_str='' || log_str="$LOG_LEVEL " | |
| log_str="${log_str}$(date "+${LOG_DATE_STRING-%Y-%m-%d %H:%M:%S}") $@" | |
| echo "$log_str" | |
| } | |
| info() { LOG_LEVEL='INFO' log "$@"; } | |
| warn() { LOG_LEVEL='WARN' log "$@"; } | |
| error() { LOG_LEVEL='ERROR' log "$@"; } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ log my message | |
| 2016-06-06 16:08:44 my message | |
| $ info my message | |
| INFO 2016-06-06 16:08:44 my message | |
| $ warn my message | |
| WARN 2016-06-06 16:08:44 my message | |
| $ error my message | |
| ERROR 2016-06-06 16:08:44 my message | |
| $ export LOG_DATE_STRING='+MY_DATE%H' | |
| $ log my message | |
| +MY_DATE16 my message | |
| $ info my message | |
| INFO +MY_DATE16 my message | |
| $ warn my message | |
| WARN +MY_DATE16 my message | |
| $ error my message | |
| ERROR +MY_DATE16 my message |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment