summaryrefslogtreecommitdiff
path: root/home/scripts/log.sh
blob: 88950971f0a0346682fbc0fc02dc7cdcbdfe3b19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/bin/sh

# usage:
# > source log.sh <CALLER>
# > log INFO "hello"
# > log "this is epic"
# > log DEBUG "yo"

_LOG_CALLER=$(basename "$1")

log() {
    LEVEL=$1
    shift

    case "$LEVEL" in
        INFO|DEBUG|ERROR)
            ;;
        *)
            set -- "INFO" "$LEVEL" "$@"
            LEVEL="INFO"
            ;;
    esac

    TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
    MESSAGE="$*"

    if [ "$LEVEL" = "DEBUG" ] && [ -z "$DEBUG" ]; then
        return
    fi

    echo "[$TIMESTAMP] [$_LOG_CALLER] [$LEVEL] $MESSAGE"
}