-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlog-helper
executable file
·123 lines (93 loc) · 2.62 KB
/
log-helper
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/bash -e
# log helper base on environment variable CONTAINER_LOG_LEVEL
# CONTAINER_LOG_LEVEL environment variable is set by run tool based on --log-level argument (info by default)
# or you can set it directly with docker --env argument
# Usage example: log-helper info CONTAINER_LOG_LEVEL is info or more
# the message "CONTAINER_LOG_LEVEL is info or more" will be printed only if log level is info, debug or trace
LOG_LEVEL_NONE=0
LOG_LEVEL_ERROR=1
LOG_LEVEL_WARNING=2
LOG_LEVEL_INFO=3
LOG_LEVEL_DEBUG=4
LOG_LEVEL_TRACE=5
# default log level if CONTAINER_LOG_LEVEL is not set -> info
log_level=${CONTAINER_LOG_LEVEL:-${LOG_LEVEL_INFO}}
call=$1 # function to call (error, warning, info, debug, trace, level)
if [[ ! "$call" =~ ^(error|warning|info|debug|trace|level)$ ]]; then
echo "Error: Function $call not found"
echo "Allowed functions are: error, warning, info, debug, trace, level"
echo "usage example: log-helper info hello !"
exit 1
fi
echo_msg="" # message to print if required log level is set
echo_param="" # echo command parameters
function error() {
getEchoParams $@
if [ $log_level -ge 1 ]; then
echo $echo_param "$echo_msg"
fi
}
function warning() {
getEchoParams $@
if [ $log_level -ge 2 ]; then
echo $echo_param "$echo_msg"
fi
}
function info() {
getEchoParams $@
if [ $log_level -ge 3 ]; then
echo $echo_param "$echo_msg"
fi
}
function debug() {
getEchoParams $@
if [ $log_level -ge 4 ]; then
echo $echo_param "$echo_msg"
fi
}
function trace() {
getEchoParams $@
if [ $log_level -ge 5 ]; then
echo $echo_param "$echo_msg"
fi
}
function getMsgFromStdin() {
if [ -z "$2" ]; then
echo_msg=$(cat)
fi
}
function getEchoParams() {
echo_msg="$@"
if [[ "$1" =~ ^(-e|-n|-E)$ ]]; then
echo_param=$1
echo_msg=${echo_msg#$1 }
fi
# read from pipe if echo_msg is empty
[[ -n "$echo_msg" ]] || getMsgFromStdin
}
function level() {
local operator=$1
local loglevel_str=$2
local loglevel_str=${loglevel_str^^} # uppercase
if [[ ! "$operator" =~ ^(eq|ne|gt|ge|lt|le)$ ]]; then
echo "Error: Operator $operator not allowed"
echo "Allowed operators are: eq, ne, gt, ge, lt, le"
echo "Help: http://www.tldp.org/LDP/abs/html/comparison-ops.html"
exit 1
fi
if [ -z "$loglevel_str" ]; then
echo "Error: No log level provided"
echo "Allowed log level are: none, error, warning, info, debug, trace"
echo "usage example: log-helper level eq info"
exit 1
fi
local log_level_var=LOG_LEVEL_$loglevel_str
if [ $log_level -$operator ${!log_level_var} ]; then
exit 0
else
exit 1
fi
}
shift
$call "$@"
exit 0