-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash-lib-logging
105 lines (88 loc) · 1.63 KB
/
bash-lib-logging
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
#!/bin/bash
#
# Bash logging library
#
__initLoggingLibrary()
{
(( ${__LOGGING_LIB_INITIALIZED__} )) && return
readonly __LOGGING_LIB_INITIALIZED__=1
readonly DEFAULT_LOG_DIR="${pwd}"
readonly DEFAULT_LOG_FILE_NAME="app.log"
readonly DEFAULT_LOG_TAG="APP-LOG"
LOG_DIR="${DEFAULT_LOG_DIR}"
LOG_FILE_NAME="${DEFAULT_LOG_FILE_NAME}"
LOG_FILE="${LOG_DIR}/${LOG_FILE_NAME}"
LOG_TAG="${DEFAULT_LOG_TAG}"
LOGGING_ENABLED=0
LOGGING_BACKUP=0
}
setLogDirectory()
{
LOG_DIR=${1:-${LOG_DIR}}
LOG_FILE="${LOG_DIR}/${LOG_FILE_NAME}"
}
setLogFileName()
{
LOG_FILE_NAME=${1:-${LOG_FILE_NAME}}
LOG_FILE="${LOG_DIR}/${LOG_FILE_NAME}"
}
setLogTag()
{
LOG_TAG=$1
}
enableLogging()
{
LOGGING_ENABLED=1
}
disableLogging()
{
LOGGING_ENABLED=0
}
enableLogBackup()
{
LOGGING_BACKUP=1
}
disableLogBackup()
{
LOGGING_BACKUP=0
}
newLogFile()
{
if [ -f "${LOG_FILE}" ]; then
if (( ${LOGGING_ENABLED} )) && (( ${LOGGING_BACKUP} )); then
local backupLogFile="${LOG_FILE}-$(ls --full-time ${LOG_FILE} | awk '{ print $6"_"$7 }' | sed 's/[.].*$//')"
if (( $(cp ${LOG_FILE} ${backupLogFile} 2>&1; echo $?) )); then
echo "WARN: Failed to create backup of the previous log file"
fi
fi
fi
if (( $(cat /dev/null > ${LOG_FILE} 2>&1; echo $?) )); then
echo "Failed to create/clear the log file [${LOG_FILE}]"
echo "Exiting..."
exit 1
fi
}
log()
{
if (( LOGGING_ENABLED )); then
echo -e "${1}" | tee -a ${LOG_FILE}
else
echo -e "${1}"
fi
}
logPipe()
{
while read message; do
log "${message}"
done
}
abort()
{
local msg=${1}
local exitCode=${2:-1}
log "${1}"
log "Aborting..."
exit ${exitCode}
}
__initLoggingLibrary
#::END::