-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-logs
executable file
·21 lines (21 loc) · 1.7 KB
/
docker-logs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/sh
if [ ! -z "${LOG_API}" ]; then\
DATE=`date --utc +%FT%T.%NZ`;\
echo logging to ${LOG_API} in background;\
docker ps -q | xargs -L 1 docker inspect -f {{.LogPath}} | \
xargs sudo egrep '^\{"log"' |\
sed 's|^/var/lib.*/\([0-9a-f]\{12\}\)[0-9a-f]*-json.log:{|{"server":"\1", |' |\
sed `docker ps | grep -v CONTAINER | awk '{printf "s|" $1 "|" $NF "|;"}'` |\
jq -c 'select(.time<="'$DATE'")|{ server: .server, log: (if (try (.log | fromjson) catch "error") == "error" then {"message": .log } else (.log | fromjson) end ) , stream: .stream, time: .time }'|\
sed 's/'"'"'/'"'"'"'"'"'"'"'"'/g;s|^|curl -s -XPOST "${LOG_API}" -H "Content-Type: application/json" -d '"'"'|;s/$/'"'"'/'|\
sh > /dev/null;\
docker ps -q | xargs -L 1 docker inspect -f {{.LogPath}} | \
xargs sudo tail -f | stdbuf -o0 sed 's|^==> /var/lib.*/\([0-9a-f]\{12\}\)[0-9a-f]*-json.log <==|\1|;s/^{//;' |\
stdbuf -o0 awk '{if (/^............$/){server=$1}else{if (NF) {print "{\"server\":\"" server "\", " $0}}}' |\
stdbuf -o0 sed `docker ps | grep -v CONTAINER | awk '{printf "s|" $1 "|" $NF "|;"}'` |\
stdbuf -o0 jq -c 'select(.time>"'$DATE'")|{ server: .server, log: (if (try (.log | fromjson) catch "error") == "error" then {"message": .log } else (.log | fromjson) end ) , stream: .stream, time: .time }'|\
stdbuf -o0 sed 's/'"'"'/'"'"'"'"'"'"'"'"'/g;s|^|curl -s -XPOST "${LOG_API}" -H "Content-Type: application/json" -d '"'"'|;s/$/'"'"'/'|\
stdbuf -o0 sh > /dev/null;\
else\
echo LOG_API endpoint has to be set;\
fi