stout, stderr to syslog via function with if statement

I have a function called sysLogger in a bash script that I am using to redirect stdout and stderr to syslog.

Part of the script contains an option to turn on debugging otherwise I want debugging output to go to /dev/null.

I am struggling to understand how to make this work using the function I have created to take stderr and stdout and redirect it to syslog. Sending output to syslog works fine (although I am seeing file descriptor left open errors, not sure if they are a problem). But if I add an "if" statement to try and redirect any output to /dev/null depending on whether debugging is enabled I get "broken pipe" errors.

I have tried using "tee" and various other options to just redirect to /dev/null but none worked and googling about process substitution and file descriptors has made more confused than anything.

The full script and config file is attached so you can see what I am trying to do, but here is the important bits.

Function:

####### logging
sysLogger () {
        local logPriority=$1
        local logMessage=$2
        case "${logPriority}" in

                critical)       logger -p local1.notice -t mugsyback.crit ${logMessage}
                ;;  
                warning)        logger -p local1.notice -t mugsyback.warn ${logMessage}
                ;;  
                info)           logger -p local1.notice -t mugsyback.info ${logMessage}
                ;;  
                debug)          if "${logLevel}" = "debug" ; then
                                        logger -p local1.notice -t mugsyback.debug ${logMessage}
                                        else > /dev/null
                                fi  
                ;;  
                *)              echo "logging option does not exist"
                ;;  
        esac
}

Once example of output being redirected (altered for purposes of this post)

mount -v /mnt/backup 2> >(sysLogger "critical") 1> >(sysLogger "debug") || { backupResult=1 ; cleanup ; }

I have the following in /etc/rsyslog.d/mugsyback.conf

cat /etc/rsyslog.d/mugsyback.conf 
# - mugsyback logging -
local1.notice                   /var/log/mugsyback.log

thanks

The function must be:

####### logging
sysLogger () {
        local logPriority=$1
        local logMessage=$2
        case "${logPriority}" in
 
                critical)       logger -p local1.notice -t mugsyback.crit ${logMessage}
                ;;  
                warning)        logger -p local1.notice -t mugsyback.warn ${logMessage}
                ;;  
                info)           logger -p local1.notice -t mugsyback.info ${logMessage}
                ;;  
                debug)          if "${logLevel}" = "debug" ; 
                                     then
                                        logger -p local1.notice -t mugsyback.debug ${logMessage}
                                     else 
                                        logger -p local1.notice -t mugsyback.debug /dev/null
                                 fi  
                ;;  
                *)              echo "logging option does not exist"
                ;;  
        esac
}

Edit: Sincere apologies, I have just realised I had not added "test" to the syntax. of the if statement. I have altered this post accordingly

Thanks, but it did not work for me.

I have created the following as a test script to simplify things.

I should also point out that the variable $logMessage is only used for manual entries into syslog otherwise the output from stderr and stdout is piped "into" logger.

e.g

sysLogger "info" "this text is added to syslog"
#!/bin/bash -x

logLevel=

####### logging
sysLogger () {
        local logPriority=$1
        local logMessage=$2
        case "${logPriority}" in

                critical)       logger -t mugsyback.crit ${logMessage}
                ;;  
                warning)        logger -t mugsyback.warn ${logMessage}
                ;; 
                info)           logger -t mugsyback.info ${logMessage}
                ;;  
                debug)	      	if test "${logLevel}" = "debug";
					then
						logger -t mugsyback.debug ${logMessage}
					else
						logger -t mugsyback.debug /dev/null
				fi
                ;;  
                *)              echo "logging option does not exist"
                ;;  
        esac
}

mount -v /mnt/backup 2> >(sysLogger "critical") 1> >(sysLogger "debug") || { echo "failed" ; exit 1 ; }

I get the following output (with bash -x)

[/C+ logLevel=
+ mount -v /mnt/backup
++ sysLogger critical
++ local logPriority=critical
++ local logMessage=
++ case "${logPriority}" in
++ logger -t mugsyback.crit
+ echo failed
failed
+ exit 1

And the following in /var/log/syslog

Sep  5 16:04:08 debvelopment mugsyback.crit: ++ sysLogger debug
Sep  5 16:04:08 debvelopment mugsyback.crit: ++ local logPriority=debug
Sep  5 16:04:08 debvelopment mugsyback.crit: ++ local logMessage=
Sep  5 16:04:08 debvelopment mugsyback.crit: ++ case "${logPriority}" in
Sep  5 16:04:08 debvelopment mugsyback.crit: ++ test '' = debug
Sep  5 16:04:08 debvelopment mugsyback.crit: ++ logger -t mugsyback.debug /dev/null
Sep  5 16:04:08 debvelopment mugsyback.debug: /dev/null
Sep  5 16:04:08 debvelopment mugsyback.crit: Broken pipe

If I undestand what you wants, it must be:
Function:

####### logging
sysLogger () {
        local logPriority=$1
        local logMessage=$2
        case "${logPriority}" in
 
                critical)       logger -p local1.notice -t mugsyback.crit ${logMessage}
                ;;  
                warning)        logger -p local1.notice -t mugsyback.warn ${logMessage}
                ;;  
                info)           logger -p local1.notice -t mugsyback.info ${logMessage}
                ;;  
                debug)          if test "${logLevel}" = "debug" ; then
                                        logger -p local1.notice -t mugsyback.debug ${logMessage}
                                fi  
                ;;  
                *)              echo "logging option does not exist"
                ;;  
        esac
}

Thanks again john1212, I had actually tried that already but it still fails with a pipe error.

I need something that handles the output like logger does but allows me to redirect it to /dev/null.

please send /var/log/syslog

Sep  6 08:44:52 debvelopment mugsyback.crit: Broken pipe
logger -p local1.notice -t mugsyback.debug TEST

please send /var/log/syslog

$ logger -p local1.notice -t mugsyback.debug TEST
$ grep TEST /var/log/syslog
Sep  6 09:41:35 debvelopment mugsyback.debug: TEST

Logger itself works fine, including in the function.

I think you want it.

cat /etc/rsyslog.d/mugsyback.conf 
# - mugsyback logging -
local1.notice                   /var/log/mugsyback.log
local9.debug			/dev/null

and

sysLogger () {
        local logPriority=$1
        local logMessage=$2
        case "${logPriority}" in

                critical)       logger -p local1.notice -t mugsyback.crit ${logMessage}
                ;;  
                warning)        logger -p local1.notice -t mugsyback.warn ${logMessage}
                ;;  
                info)           logger -p local1.notice -t mugsyback.info ${logMessage}
                ;;  
                debug)          if "${logLevel}" = "debug" ; then
                                        logger -p local1.notice -t mugsyback.debug ${logMessage}
                                        else 
                                        logger -p local9.notice -t mugsyback.debug ${logMessage}
                                fi  
                ;;  
                *)              echo "logging option does not exist"
                ;;  
        esac
}
1 Like

Nice work!

I simply hadn't thought of using syslog itself to send it to /dev/null.

That will work fine for my script.

Thankyou very much for taking the time to respond John1212, this is what I ended up using as a test:

$ cat /tmp/mugsyback.test
#!/bin/bash

logLevel=

####### logging
sysLogger () {
        local logPriority=$1
        local logMessage=$2
        case "${logPriority}" in

                critical)       logger -p local1.notice -t mugsyback.crit ${logMessage}
                ;;  
                warning)        logger -p local1.notice -t mugsyback.warn ${logMessage}
                ;;  
                info)           logger -p local1.notice -t mugsyback.info ${logMessage}
                ;;  
                debug)          if test "${logLevel}" = "debug" ;
					then
                                        	logger -p local1.notice -t mugsyback.debug ${logMessage}
                                        else	
						logger -p local1.debug -t mugsyback.debug ${logMessage}
                                fi  
                ;;  
                *)              echo "logging option does not exist"
                ;;  
        esac
}

mount -v /mnt/backup 2> >(sysLogger "critical") 1> >(sysLogger "debug") || { echo "failed" ; exit 1 ; }

And for syslog:

$ cat /etc/rsyslog.d/mugsyback.conf 
# - mugsyback logging -
# log all debug messages to /dev/null unless debugging enabled
# log everything else to /var/log/mugsyback.log
local1.notice	/var/log/mugsyback.log
local1.debug	/dev/null

I still get the file descriptor errors though so that's what I need to figure out next. Here is what the debug looks like now and shows at what stage the file descriptor errors occur (I haven't tidied up the logging yet)...

Sep  7 21:45:36 debvelopment mugsyback.info: Begin mugsyback at 1283859936
Sep  7 21:45:36 debvelopment mugsyback.debug: /dev/sdb1 on /mnt/backup type ext3 (rw)
Sep  7 21:45:36 debvelopment mugsyback.debug: check file found, continuing...
Sep  7 21:45:37 debvelopment mugsyback.debug: discovered 3 Backup Sources
Sep  7 21:45:37 debvelopment mugsyback.debug: "/mnt/backup/BACKUP/debvelopment" exists and contains folders
Sep  7 21:45:37 debvelopment mugsyback.debug: Removing /mnt/backup/BACKUP/debvelopment/2010-09-071283859469
Sep  7 21:45:37 debvelopment mugsyback.info: === BACKUP REPORT FOR "boot" ===
Sep  7 21:45:37 debvelopment mugsyback.info: Backup Engine = tar
Sep  7 21:45:48 debvelopment mugsyback.info: Backup Status: Success
Sep  7 21:45:49 debvelopment mugsyback.info: Number of files: 18
Sep  7 21:45:49 debvelopment mugsyback.info: Total file size: 106M bytes
Sep  7 21:45:49 debvelopment mugsyback.crit: File descriptor 60 left open
Sep  7 21:45:49 debvelopment mugsyback.crit: File descriptor 61 left open
Sep  7 21:45:49 debvelopment mugsyback.crit: File descriptor 62 left open
Sep  7 21:45:49 debvelopment mugsyback.crit: File descriptor 63 left open
Sep  7 21:45:50 debvelopment mugsyback.debug: "/mnt/root-snapshot" exists, not creating it...
Sep  7 21:45:50 debvelopment mugsyback.debug:   Logical volume "root-snapshot" created
Sep  7 21:45:50 debvelopment mugsyback.debug: mount: you didn't specify a filesystem type for /dev/mapper/vg00-root--snapshot
Sep  7 21:45:50 debvelopment mugsyback.debug:        I will try type ext3
Sep  7 21:45:50 debvelopment mugsyback.debug: /dev/mapper/vg00-root--snapshot on /mnt/root-snapshot type ext3 (rw)
Sep  7 21:45:50 debvelopment mugsyback.info: === BACKUP REPORT FOR "root" ===
Sep  7 21:45:50 debvelopment mugsyback.info: Backup Engine = tar
Sep  7 21:46:09 debvelopment mugsyback.info: Backup Status: Success
Sep  7 21:46:09 debvelopment mugsyback.info: Number of files: 2774
Sep  7 21:46:10 debvelopment mugsyback.info: Total file size: 4.8M bytes
Sep  7 21:46:10 debvelopment mugsyback.debug: /dev/mapper/vg00-root--snapshot umounted
Sep  7 21:46:10 debvelopment mugsyback.crit: File descriptor 60 left open
Sep  7 21:46:10 debvelopment mugsyback.crit: File descriptor 61 left open
Sep  7 21:46:10 debvelopment mugsyback.crit: File descriptor 62 left open
Sep  7 21:46:10 debvelopment mugsyback.crit: File descriptor 63 left open
Sep  7 21:46:11 debvelopment mugsyback.debug:   Logical volume "root-snapshot" successfully removed
Sep  7 21:46:11 debvelopment mugsyback.crit: File descriptor 60 left open
Sep  7 21:46:11 debvelopment mugsyback.crit: File descriptor 61 left open
Sep  7 21:46:11 debvelopment mugsyback.crit: File descriptor 62 left open
Sep  7 21:46:11 debvelopment mugsyback.crit: File descriptor 63 left open
Sep  7 21:46:11 debvelopment mugsyback.debug: "/mnt/var-data-snapshot" exists, not creating it...
Sep  7 21:46:11 debvelopment mugsyback.debug:   Logical volume "var-data-snapshot" created
Sep  7 21:46:12 debvelopment mugsyback.debug: mount: you didn't specify a filesystem type for /dev/mapper/vg00-var--data--snapshot
Sep  7 21:46:12 debvelopment mugsyback.debug:        I will try type xfs
Sep  7 21:46:12 debvelopment mugsyback.debug: /dev/mapper/vg00-var--data--snapshot on /mnt/var-data-snapshot type xfs (ro,nouuid)
Sep  7 21:46:12 debvelopment mugsyback.info: 
Sep  7 21:46:12 debvelopment mugsyback.info: Number of files: 5
Sep  7 21:46:12 debvelopment mugsyback.info: Number of files transferred: 0
Sep  7 21:46:12 debvelopment mugsyback.info: Total file size: 805.31M bytes
Sep  7 21:46:12 debvelopment mugsyback.info: Total transferred file size: 0 bytes
Sep  7 21:46:12 debvelopment mugsyback.info: Literal data: 0 bytes
Sep  7 21:46:12 debvelopment mugsyback.info: Matched data: 0 bytes
Sep  7 21:46:12 debvelopment mugsyback.info: File list size: 111
Sep  7 21:46:12 debvelopment mugsyback.info: File list generation time: 0.003 seconds
Sep  7 21:46:12 debvelopment mugsyback.info: File list transfer time: 0.000 seconds
Sep  7 21:46:12 debvelopment mugsyback.info: Total bytes sent: 126
Sep  7 21:46:12 debvelopment mugsyback.info: Total bytes received: 14
Sep  7 21:46:12 debvelopment mugsyback.info: 
Sep  7 21:46:12 debvelopment mugsyback.info: sent 126 bytes  received 14 bytes  280.00 bytes/sec
Sep  7 21:46:12 debvelopment mugsyback.info: total size is 805.31M  speedup is 5752188.34
Sep  7 21:46:12 debvelopment mugsyback.debug: /dev/mapper/vg00-var--data--snapshot umounted
Sep  7 21:46:12 debvelopment mugsyback.crit: File descriptor 60 left open
Sep  7 21:46:12 debvelopment mugsyback.crit: File descriptor 61 left open
Sep  7 21:46:12 debvelopment mugsyback.crit: File descriptor 62 left open
Sep  7 21:46:12 debvelopment mugsyback.crit: File descriptor 63 left open
Sep  7 21:46:13 debvelopment mugsyback.debug:   Logical volume "var-data-snapshot" successfully removed
Sep  7 21:46:13 debvelopment mugsyback.debug: /dev/sdb1 umounted
Sep  7 21:46:13 debvelopment mugsyback.info: === BACKUP MEDIA USAGE ===
Sep  7 21:46:13 debvelopment mugsyback.info: Filesystem Size Used Avail Use% Mounted on
Sep  7 21:46:13 debvelopment mugsyback.info: /dev/sdb1 8.5G 1.5G 6.7G 18% /mnt/backup
Sep  7 21:46:13 debvelopment mugsyback.debug: end mugsyback at 1283859973