How to give a multi-lines output to logger?

Technical issues/questions of an intermediate or advanced nature.
User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

How to give a multi-lines output to logger?

Post#1 by Rava » 07 Sep 2022, 06:31

Is there a way to easy give a multi-lines output to logger so that it not uses that as one unbroken by newlines output, but inserts it via different logging instances and convert tabs into whitespaces to keep the meant layout.

For example, the output of my script /usr/local/bin/fxsx

Code: Select all

#!/bin/bash
#fxsx V0.1 - like "fx|head -n 3;sx|tail -n 3"
function sx () 
{ 
    echo $(date +%d.%m.%Y\ %H:%M:%S) ____________________________________________________________;
    { 
        read firstLine;
        echo "$firstLine";
        while read f t s u p; do
            let "s2 = $s / 1024";
            let "u2 = $u / 1024";
            printf '%-40s%-16s%-8s%-8s%-8s\n' $f $t $s2 $u2 $p;
        done
    } < /proc/swaps
}

function fx ()
{	echo $(date +%d.%m.%Y\ %H:%M:%S) ____________________________________________________________;
	/bin/free -m
}

fx|head -n 4;t;sx|tail -n 3
(it also uses my /usr/local/bin/t to draw a vertical line the width of $COLUMNS)

fxsx itself looks like so in the terminal

Code: Select all

root@porteus:/# fxsx
07.09.2022 07:05:02 ____________________________________________________________
               total        used        free      shared  buff/cache   available
Mem:            3873        2208         803         163         862        1072
Swap:           5382         535        4847
________________________________________________________________________________
Filename				Type		Size	Used	Priority
/dev/sda6                               partition       4607    0       1       
/dev/zram0                              partition       774     535     100     
but when simply given to logger via

Code: Select all

logger $(fxsx)
it looks messy in /var/log/messages:

Code: Select all

Sep  7 06:57:12 porteus syslogd[690]: -- MARK --
Sep  7 07:05:12 porteus root: 07.09.2022 07:05:12 ____________________________________________________________ total used free shared buff/cache available Mem: 3873 2212 799 163 862 1068 Swap: 5382 535 4847 ^[[33m^[[1m________________________________________________________________________________^[[m Filename Type Size Used Priority /dev/sda6 partition 4607 0 1 /dev/zram0 partition 774 535 100
Sep  7 07:05:22 porteus ModemManager[1596]: <info>  [sleep-monitor] system is about to suspend For clarity, I left the line before and the line after that in as well. 
The "^[[33m^[[1m" is because of the coloured divider line that logger or /var/log/messages can't handle, and my script is too stupid to know if the output is sent via logger to suppress any colours, but these parts are the most minor ones, compared with the messed up newlines and messed up tabs.
Cheers!
Yours Rava