r/bash•
TooOldForShaadi

What is your opinion about these logger functions I came up with?

critique
function _log() {
	local level="$1"
	local color="$2"
	local stream="$3"
	shift 3

	local fmt="$1"
	shift

	local reset='\033[0m'
	local timestamp
	timestamp="$(date -u '+%Y-%m-%d %H:%M:%S %z')"
	local colored_level="${color}${level}${reset}"

	local message
	# shellcheck disable=SC2059
	printf -v message -- "${fmt}" "$@"

	local line="[${timestamp}] ${colored_level} ${message}"

	if [[ "${stream}" == "stderr" ]]; then
		printf '%b\n' "${line}" >&2
	else
		printf '%b\n' "${line}"
	fi
}

function log_info() {
	local blue='\033[0;34m'
	_log "INFO" "${blue}" "stdout" "$@"
}

function log_warn() {
	local yellow='\033[0;33m'
	_log "WARN" "${yellow}" "stdout" "$@"
}

function log_error() {
	local red='\033[0;31m'
	_log "ERROR" "${red}" "stderr" "$@"
}

Basically I would like to have a logger sorta thing, you know with INFO, ERROR and WARN for now with different colors etc for bash

  • It should be able to handle log_info "%s is %d" "number" 10
  • Apart from printing timestamps, redirecting error logs to stderr etc

What do you think about this implementation? Good enough or needs improvements?

Could ask an AI easily but I am looking for human opinions here

Posted Apr 29, 2026, 7:08 AM UTC

46
Loading comments…