parent
2444c7db21
commit
8e4ffe5024
@ -1,3 +1,6 @@
|
|||||||
# utils
|
# README
|
||||||
|
|
||||||
Collection of scripts and functions to make my developer life easier.
|
Collection of scripts and functions to make my developer life easier.
|
||||||
|
|
||||||
|
**bash/logger**
|
||||||
|
*Enrich command output for logging purposes*
|
||||||
|
@ -0,0 +1,25 @@
|
|||||||
|
# LOGGER
|
||||||
|
|
||||||
|
Enrich command output for logging purpose.
|
||||||
|
This works by sending stdout and stderr to named pipes.
|
||||||
|
The content is read from the pipes and enriched before writing to a file.
|
||||||
|
There are three enrichment examples in logger.sh:
|
||||||
|
|
||||||
|
- plain text
|
||||||
|
- json format
|
||||||
|
- systemd journal
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
Add the commands you want to run in the ./writer.sh file.
|
||||||
|
Start the process via ./controller.sh.
|
||||||
|
|
||||||
|
``` bash
|
||||||
|
./controller.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
The last command in ./writer.sh ensures that the named pipes get closed and removed in a save way!
|
||||||
|
|
||||||
|
``` bash
|
||||||
|
echo "quit" >$pipe 2>$err_pipe
|
||||||
|
```
|
@ -0,0 +1,34 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# Author: Nils Gerstner
|
||||||
|
|
||||||
|
#------------- V A R I A B L E S -------------#
|
||||||
|
|
||||||
|
LOGFILE="./update.log"
|
||||||
|
PID=$$
|
||||||
|
pipe=/tmp/out_pipe-$PID
|
||||||
|
err_pipe=/tmp/err_pipe-$PID
|
||||||
|
|
||||||
|
#--------- G R A C E F U L E X I T ---------#
|
||||||
|
|
||||||
|
trap "rm -f $pipe $err_pipe; pkill -P $PID;" EXIT
|
||||||
|
|
||||||
|
#---------- S T A R T L O G G E R ----------#
|
||||||
|
|
||||||
|
# Start logging script
|
||||||
|
./logger.sh $pipe $err_pipe |tee -a ./update.log &
|
||||||
|
|
||||||
|
# Make sure named pipes have been created
|
||||||
|
count=0
|
||||||
|
until [[ -p $pipe ]] || [[ -p $err_pipe ]]; do
|
||||||
|
((count++))
|
||||||
|
if [ $count -gt 100 ]; then break; fi
|
||||||
|
sleep 0.001
|
||||||
|
done
|
||||||
|
sleep 1
|
||||||
|
|
||||||
|
#------------ R U N S C R I P T ------------#
|
||||||
|
|
||||||
|
./writer.sh $pipe $err_pipe
|
||||||
|
|
||||||
|
wait
|
||||||
|
exit 0
|
@ -0,0 +1,71 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# Author: Nils Gerstner
|
||||||
|
|
||||||
|
pipe="$1"
|
||||||
|
err_pipe="$2"
|
||||||
|
logfile="$3"
|
||||||
|
pid=""
|
||||||
|
err_pid=""
|
||||||
|
TAB="$(printf '\t')"
|
||||||
|
|
||||||
|
trap ">&2 echo 'Logger exiting'; ps -p $pid 2>/dev/null && kill -9 $pid; ps -p $err_pid 2> /dev/null && kill -9 $err_pid; rm -f $err_pipe $pipe 2>&1 > /dev/null" EXIT
|
||||||
|
|
||||||
|
if [[ ! -p $pipe ]] || [[ ! -p $err_pipe ]]; then
|
||||||
|
mkfifo $pipe
|
||||||
|
mkfifo $err_pipe
|
||||||
|
while [[ ! -p $pipe ]] && [[ ! -p $err_pipe ]]; do
|
||||||
|
sleep 0.01
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
|
log() {
|
||||||
|
local named_pipe="$1"
|
||||||
|
local other_pipe="$2"
|
||||||
|
local level="$3"
|
||||||
|
|
||||||
|
while true; do
|
||||||
|
in_msg="$(cat $named_pipe)"
|
||||||
|
if [ -n "$in_msg" ]; then
|
||||||
|
if [[ "$in_msg" == 'quit' ]]; then
|
||||||
|
>&2 echo "stopping $named_pipe"
|
||||||
|
echo "quit" > $other_pipe
|
||||||
|
rm -f $named_pipe
|
||||||
|
break
|
||||||
|
else
|
||||||
|
#----- L O G E X A M P L E S -----#
|
||||||
|
#
|
||||||
|
# Log to plain text
|
||||||
|
# cat >> "${logfile}_plain" <<< "${level}${TAB}${in_msg}"
|
||||||
|
|
||||||
|
# Log to json file
|
||||||
|
jq -n -c --arg msg "${in_msg}" --arg level "${level}" '{ "level": $level, "timestamp": (now | strftime("%Y-%m-%d %H:%M:%S")), "message": $msg }' |tee -a $logfile
|
||||||
|
|
||||||
|
# Log to systemd journal
|
||||||
|
# logger -t My-Logger -p ${level,,} "${msg}"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
log_info(){
|
||||||
|
log $pipe $err_pipe INFO
|
||||||
|
}
|
||||||
|
|
||||||
|
log_err(){
|
||||||
|
log $err_pipe $pipe ERROR
|
||||||
|
}
|
||||||
|
|
||||||
|
while [[ ! -p $pipe ]] || [[ ! -p $err_pipe ]]; do
|
||||||
|
sleep 0.1
|
||||||
|
done
|
||||||
|
|
||||||
|
log_info & pid="$!"
|
||||||
|
log_err & err_pid="$!"
|
||||||
|
while [[ -p $pipe ]] && [[ -p $err_pipe ]]; do
|
||||||
|
sleep 0.01
|
||||||
|
done
|
||||||
|
ps -p $pid && kill -9 $pid
|
||||||
|
ps -p $err_pid && kill -9 $err_pid
|
||||||
|
|
||||||
|
wait
|
||||||
|
exit 0
|
@ -0,0 +1,11 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# Author: Nils Gerstner
|
||||||
|
|
||||||
|
pipe=$1
|
||||||
|
err_pipe=$2
|
||||||
|
|
||||||
|
echo "$$ The time is $(date)" >$pipe 2>$err_pipe
|
||||||
|
edf "$$ The time is $(date)" >$pipe 2>$err_pipe
|
||||||
|
ls |tail -n1 >$pipe 2>$err_pipe
|
||||||
|
echo "$$ The time is $(date)" 2>$err_pipe
|
||||||
|
echo "quit" >$pipe 2>$err_pipe
|
Loading…
Reference in new issue