-
Notifications
You must be signed in to change notification settings - Fork 1
/
00_logger
66 lines (55 loc) · 1.07 KB
/
00_logger
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash
ERROR=0
WARN=1
INFO=2
DEBUG=3
LOG_LEVEL=${LOG_LEVEL:-$INFO}
log() {
local log_level_int=$1
if [[ $log_level_int -gt $LOG_LEVEL ]]; then
return
fi
local log_level_str=""
case $log_level_int in
$ERROR)
# Red
color="31"
log_level_str="ERROR"
;;
$WARN)
# Yellow
color="33"
log_level_str="WARN"
;;
$DEBUG)
# Cyan
color="36"
log_level_str="DEBUG"
;;
$INFO)
# Green
color="32"
log_level_str="INFO"
;;
*)
# Use default color for unrecognized log level
color="38"
;;
esac
local message=${@:2}
local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
local log_message="[$timestamp][$log_level_str] $message"
echo -e "\033[1;${color}m${log_message}\033[0m"
}
error() {
log $ERROR $@
}
warn() {
log $WARN $@
}
info() {
log $INFO $@
}
debug() {
log $DEBUG $@
}