-
Notifications
You must be signed in to change notification settings - Fork 3
/
lj
executable file
·155 lines (133 loc) · 3.59 KB
/
lj
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env zsh
# Create a mapping of log levels to their names
typeset -A _log_levels
_log_levels=(
'emergency' 0
'alert' 1
'critical' 2
'error' 3
'warning' 4
'notice' 5
'info' 6
'debug' 7
)
###
# Output usage information and exit
###
function _lumberjack_usage() {
echo "\033[0;33mUsage:\033[0;m"
echo " lj [options] [<level>] <message>"
echo
echo "\033[0;33mOptions:\033[0;m"
echo " -h, --help Output help text and exit"
echo " -v, --version Output version information and exit"
echo " -f, --file Set the logfile and exit"
echo " -l, --level Set the log level and exit"
echo
echo "\033[0;33mLevels:\033[0;m"
echo " emergency"
echo " alert"
echo " critical"
echo " error"
echo " warning"
echo " notice"
echo " info"
echo " debug"
}
###
# Output the message to the logfile
###
function _lumberjack_message() {
local level="$1" file="$2" logtype="$3" msg="${(@)@:4}"
# If the file string is empty, output an error message
if [[ -z $file ]]; then
echo "\033[0;31mNo logfile has been set for this process. Use \`lumberjack --file /path/to/file\` to set it\033[0;m"
exit 1
fi
# If the level is not set, assume 5 (notice)
if [[ -z $level ]]; then
level=5
fi
case $logtype in
# If a valid logtype is passed
emergency|alert|critical|error|warning|notice|info|debug )
# We do nothing here
;;
# In all other cases
* )
# Second argument was not a log level, so manually set it to notice
# and include the first parameter in the message
logtype='notice'
msg="${(@)@:3}"
;;
esac
if [[ $_log_levels[$logtype] > $level ]]; then
# The message being recorded is for a higher log level than the one
# currently being recorded, so gracefully exit
exit 0
fi
# Output the message to the logfile
echo "[$(echo $logtype | tr '[a-z]' '[A-Z]')] [$(date '+%Y-%m-%d %H:%M:%S')] $msg" >> $file
}
###
# The main lumberjack process
###
function _lumberjack() {
local help version logfile loglevel dir statefile state
# Create the state directory if it doesn't exist
dir="${ZDOTDIR:-$HOME}/.lumberjack"
if [[ ! -d $dir ]]; then
mkdir -p $dir
fi
# If a statefile already exists, load the level and file
statefile="$dir/$PPID"
if [[ -f $statefile ]]; then
state=$(cat $statefile)
level="$state[1]"
file="${(@)state:2}"
fi
# Parse CLI options
zparseopts -D h=help -help=help \
v=version -version=version \
f:=logfile -file:=logfile \
l:=loglevel -level:=loglevel
# If the help option is passed, output usage information and exit
if [[ -n $help ]]; then
_lumberjack_usage
exit 0
fi
# If the version option is passed, output the version and exit
if [[ -n $version ]]; then
echo "0.1.1"
exit 0
fi
# If the logfile option is passed, set the current logfile
# for the parent process ID
if [[ -n $logfile ]]; then
shift logfile
file=$logfile
# Create the log file if it doesn't exist
if [[ ! -f $file ]]; then
touch $file
fi
fi
# If the loglevel option is passed, set the current loglevel
# for the parent process ID
if [[ -n $loglevel ]]; then
shift loglevel
level=$_log_levels[$loglevel]
fi
if [[ -z $level ]]; then
level=5
fi
# Check if we're setting options rather than logging
if [[ -n $logfile || -n $loglevel ]]; then
# Store the state
echo "$level $file" >! $statefile
# Exit gracefully
exit 0
fi
# Log the message
_lumberjack_message "$level" "$file" "$@"
}
_lumberjack "$@"