1
0
mirror of https://github.com/zplug/zplug synced 2025-04-05 15:19:13 +02:00
zplug/base/log/format.zsh
2017-01-08 21:15:28 +09:00

33 lines
945 B
Bash

__zplug::log::format::with_json()
{
local -i i=1
local level="${1:-"INFO"}" message="$2"
local is_message_json=false
# Spit out to JSON
builtin printf '{'
builtin printf '"pid":%d,' "$$"
builtin printf '"shlvl":%d,' "$SHLVL"
builtin printf '"level":"%s",' "$level"
builtin printf '"dir":"%s",' "$PWD"
builtin printf '"message":'
if $is_message_json; then
builtin printf "$message"
else
builtin printf "$message" \
| __zplug::utils::ansi::remove \
| __zplug::utils::shell::json_escape \
| tr -d '\n'
fi
builtin printf ','
builtin printf '"trace":['
for ((i = 1; i < $#functrace; i++))
do
builtin printf '"%s",' "$functrace[$i]"
done
builtin printf '"%s"' "$functrace[$#functrace]"
builtin printf "],"
builtin printf '"date":"%s"' "$(strftime "%FT%T%z" $EPOCHSECONDS)"
builtin printf "}\n"
}