From 8e4ffe50243fb1acccb58abdb4487e4aa52a0666 Mon Sep 17 00:00:00 2001 From: Nils Gerstner Date: Tue, 15 Nov 2022 10:53:38 +0100 Subject: [PATCH] Add logger --- README.md | 7 ++-- bash/logger/README.md | 25 ++++++++++++++ bash/logger/controller.sh | 34 +++++++++++++++++++ bash/logger/logger.sh | 71 +++++++++++++++++++++++++++++++++++++++ bash/logger/writer.sh | 11 ++++++ 5 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 bash/logger/README.md create mode 100755 bash/logger/controller.sh create mode 100755 bash/logger/logger.sh create mode 100755 bash/logger/writer.sh diff --git a/README.md b/README.md index 7f035a4..cdd9a2e 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ -# utils +# README -Collection of scripts and functions to make my developer life easier. \ No newline at end of file +Collection of scripts and functions to make my developer life easier. + +**bash/logger** +*Enrich command output for logging purposes* diff --git a/bash/logger/README.md b/bash/logger/README.md new file mode 100644 index 0000000..1f4af5e --- /dev/null +++ b/bash/logger/README.md @@ -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 +``` diff --git a/bash/logger/controller.sh b/bash/logger/controller.sh new file mode 100755 index 0000000..309778a --- /dev/null +++ b/bash/logger/controller.sh @@ -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 diff --git a/bash/logger/logger.sh b/bash/logger/logger.sh new file mode 100755 index 0000000..9e589bd --- /dev/null +++ b/bash/logger/logger.sh @@ -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 diff --git a/bash/logger/writer.sh b/bash/logger/writer.sh new file mode 100755 index 0000000..d693c85 --- /dev/null +++ b/bash/logger/writer.sh @@ -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