-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.sh
61 lines (44 loc) · 962 Bytes
/
log.sh
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
# PUBLIC API
##
# Log general information.
LOG_info() {
local msg="$1"
_LOG_write 'INFO' "$msg"
return 0
}
##
# Log warnings.
LOG_warn() {
local msg="$1"
_LOG_write 'WARNING' "$msg"
return 0
}
##
# Log errors.
LOG_error() {
local msg="$1"
_LOG_write 'ERROR' "$msg"
return 1
}
##
# Log errors that cause the script to die.
LOG_fatal() {
local msg="$1"
_LOG_write 'FATAL' "$msg"
return 1
}
# PRIVATE API
_LOG_write() {
if [[ -z $LOG_directory ]]; then
echo "You must set LOG_directory='DIR' in your script!"
return 1
fi
local mode="$1"
local msg="$2"
local program="${BASH_SOURCE[${#BASH_SOURCE[@]}-1]##*/}"
local file="$LOG_directory/${program}_$( date +%Y%m%d ).log"
local date=$( date "+%Y-%m-%d %X" )
[[ -d $LOG_directory ]] || mkdir -p "$LOG_directory"
[[ -f $file ]] || > "$file"
echo "${mode:0:1}, [$date] $mode -- : $msg" >> "$file"
}