Skip to content

Instantly share code, notes, and snippets.

@rajiteh
Last active July 30, 2022 21:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajiteh/3a94a393d6bb486f00ccef7c92f47ed7 to your computer and use it in GitHub Desktop.
Save rajiteh/3a94a393d6bb486f00ccef7c92f47ed7 to your computer and use it in GitHub Desktop.
Colourful Bash
#!/bin/bash
[ -z "${LOGGING_NO_COLORS}" ] && [ -t 1 ] && [ -n "$(tput colors)" ] || LOGGING_NO_COLORS="TRUE"
log() {
local color=$1;shift
local msg="$(date +%T.%3N) ${BASH_SOURCE[2]##*/}[${BASH_LINENO[1]}]: ${@}"
local reset="\e[0m"
if [ -n "${LOGGING_NO_COLORS}" ]; then
color="";reset=""
fi
>&2 printf "${color}${msg}${reset}\n"
}
log_d() { log "\e[90m" "DEBUG - ${@}"; }
log_i() { log "\e[32m" "INFO - ${@}"; }
log_w() { log "\e[33m" "WARN - ${@}"; }
log_e() { log "\e[31m" "ERROR - ${@}"; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment