Last active
August 1, 2024 13:57
-
-
Save Alveel/e6a338f42abee1b3d39fdd00e0b38276 to your computer and use it in GitHub Desktop.
Bash library to echo messages with a colour depending on the provided log level
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
#!/usr/bin/env sh | |
BLUE='\033[0;34m' | |
NC='\033[0m' # No Color | |
log() { | |
# Log levels: error (red), info (green), warn (yellow), critical (bold red) | |
case $1 in | |
error) level='\033[0;31mERROR' ;; | |
info) level='\033[0;32mINFO' ;; | |
warn) level='\033[0;33mWARN' ;; | |
critical) level='\033[1;31mCRITICAL' ;; | |
*) return 111 | |
esac | |
# Shift arguments so that $* is only the message | |
shift | |
# Colourised timestamp | |
ts="[$BLUE$(date '+%F %T %Z')$NC]" | |
# Log message | |
printf "%b %b: %s$NC\n" "$ts" "$level" "$*" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment