container-library/overlay/usr/local/lib/log.sh

31 lines
798 B
Bash
Raw Normal View History

2021-10-09 11:51:41 +00:00
#!/usr/bin/env sh
# Library for logging functions
# Log the given message at the given level. All logs are written to stderr with a timestamp.
log() {
level="$1"
message="$2"
timestamp=$(date +"%Y-%m-%d %H:%M:%S")
script_name="$(basename "$0")"
2021-10-09 13:14:19 +00:00
printf >&2 "%s [%s] [%s] %b\n" "${timestamp}" "${level}" "$script_name" "${message}"
2021-10-09 11:51:41 +00:00
}
# Log the given message at INFO level. All logs are written to stderr with a timestamp.
log_info() {
message="$1"
log "INFO" "$message"
}
# Log the given message at WARN level. All logs are written to stderr with a timestamp.
log_warn() {
message="$1"
log "WARN" "$message"
}
# Log the given message at ERROR level. All logs are written to stderr with a timestamp.
log_error() {
message="$1"
log "ERROR" "$message"
}