-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathulog
executable file
·43 lines (37 loc) · 1.18 KB
/
ulog
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
#!/bin/zsh
# ~/bin/ulog
if [[ $# -ne 2 ]]; then
echo "Usage: ulog <location> <message>"
echo "Example: ulog 'controller#method' 'your message'"
exit 1
fi
# Find the project root directory by looking for .git
function find_project_root() {
local dir=$PWD
while [[ $dir != "/" ]]; do
if [[ -d $dir/.git ]]; then
echo $dir
return 0
fi
dir=${dir:h} # zsh way to get parent directory
done
echo $PWD # Return current directory if .git is not found
}
# Ensure the log file and directory exist
log_file=~/work/unified.log
mkdir -p ${log_file:h} # Create work directory if it doesn't exist
touch $log_file # Create log file if it doesn't exist
# Get service name from project root directory
project_root=$(find_project_root)
service=${project_root:t} # zsh way to get basename
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
# Get the two arguments
location=$1
message=$2
# Output with new format and different colors
# \e[37m: white for timestamp
# \e[33m: yellow for service
# \e[36m: cyan for location
echo -e "\n\e[37m$timestamp\e[0m [\e[33m$service\e[0m][\e[36m$location\e[0m]" >> $log_file
echo "$message" >> $log_file
echo "" >> $log_file