Skip to content

Instantly share code, notes, and snippets.

@Alveel
Last active August 1, 2024 13:57
Show Gist options
  • Save Alveel/e6a338f42abee1b3d39fdd00e0b38276 to your computer and use it in GitHub Desktop.
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
#!/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