forked from deanrather/bash-logger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash-logger.sh
executable file
·103 lines (92 loc) · 3.9 KB
/
bash-logger.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
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
#!/bin/bash
#--------------------------------------------------------------------------------------------------
# Bash Logger
# Copyright (c) Dean Rather
# Licensed under the MIT license
# http://github.com/deanrather/bash-logger
#--------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------
# Configurables
export LOGFILE=~/bash-logger.log
export LOG_FORMAT='%DATE %PID [%LEVEL] %MESSAGE'
export LOG_DATE_FORMAT='+%F %T %Z' # Eg: 2014-09-07 21:51:57 EST
export LOG_COLOR_DEBUG="\033[0;37m" # Gray
export LOG_COLOR_INFO="\033[0m" # White
export LOG_COLOR_NOTICE="\033[1;32m" # Green
export LOG_COLOR_WARNING="\033[1;33m" # Yellow
export LOG_COLOR_ERROR="\033[1;31m" # Red
export LOG_COLOR_CRITICAL="\033[44m" # Blue Background
export LOG_COLOR_ALERT="\033[43m" # Yellow Background
export LOG_COLOR_EMERGENCY="\033[41m" # Red Background
export RESET_COLOR="\033[0m"
#--------------------------------------------------------------------------------------------------
# Individual Log Functions
# These can be overwritten to provide custom behavior for different log levels
DEBUG() { LOG_HANDLER_DEFAULT "$FUNCNAME" "$@"; }
INFO() { LOG_HANDLER_DEFAULT "$FUNCNAME" "$@"; }
NOTICE() { LOG_HANDLER_DEFAULT "$FUNCNAME" "$@"; }
WARNING() { LOG_HANDLER_DEFAULT "$FUNCNAME" "$@"; }
ERROR() { LOG_HANDLER_DEFAULT "$FUNCNAME" "$@"; exit 1; }
CRITICAL() { LOG_HANDLER_DEFAULT "$FUNCNAME" "$@"; exit 1; }
ALERT() { LOG_HANDLER_DEFAULT "$FUNCNAME" "$@"; exit 1; }
EMERGENCY() { LOG_HANDLER_DEFAULT "$FUNCNAME" "$@"; exit 1; }
#--------------------------------------------------------------------------------------------------
# Helper Functions
# Outputs a log formatted using the LOG_FORMAT and DATE_FORMAT configurables
# Usage: FORMAT_LOG <log level> <log message>
# Eg: FORMAT_LOG CRITICAL "My critical log"
FORMAT_LOG() {
local level="$1"
local log="$2"
local pid=$$
local date="$(date "$LOG_DATE_FORMAT")"
local formatted_log="$LOG_FORMAT"
formatted_log="${formatted_log/'%MESSAGE'/$log}"
formatted_log="${formatted_log/'%LEVEL'/$level}"
formatted_log="${formatted_log/'%PID'/$pid}"
formatted_log="${formatted_log/'%DATE'/$date}"
echo "$formatted_log\n"
}
# Calls one of the individual log functions
# Usage: LOG <log level> <log message>
# Eg: LOG INFO "My info log"
LOG() {
local level="$1"
local log="$2"
local log_function_name="${!level}"
$log_function_name "$log"
}
#--------------------------------------------------------------------------------------------------
# Log Handlers
# All log levels call this handler (by default...), so this is a great place to put any standard
# logging behavior
# Usage: LOG_HANDLER_DEFAULT <log level> <log message>
# Eg: LOG_HANDLER_DEFAULT DEBUG "My debug log"
LOG_HANDLER_DEFAULT() {
# $1 - level
# $2 - message
local formatted_log="$(FORMAT_LOG "$@")"
LOG_HANDLER_COLORTERM "$1" "$formatted_log"
LOG_HANDLER_LOGFILE "$1" "$formatted_log"
}
# Outputs a log to the stdout, colourised using the LOG_COLOR configurables
# Usage: LOG_HANDLER_COLORTERM <log level> <log message>
# Eg: LOG_HANDLER_COLORTERM CRITICAL "My critical log"
LOG_HANDLER_COLORTERM() {
local level="$1"
local log="$2"
local color_variable="LOG_COLOR_$level"
local color="${!color_variable}"
log="$color$log$RESET_COLOR"
echo -en "$log"
}
# Appends a log to the configured logfile
# Usage: LOG_HANDLER_LOGFILE <log level> <log message>
# Eg: LOG_HANDLER_LOGFILE NOTICE "My critical log"
LOG_HANDLER_LOGFILE() {
local level="$1"
local log="$2"
local log_path="$(dirname "$LOGFILE")"
[ -d "$log_path" ] || mkdir -p "$log_path"
echo "$log" >> "$LOGFILE"
}