tee and functions

Greetings!

My apologies if this has been answered elsewhere before. What I have is a function (as below) set up to append to either an error log or info log based upon input.

myLOGGER ()
	{
	if [[ $( echo "$1" | egrep -w error ) ]]; then
        logfile=$elog
        lastERROR="$1"  #used elsewhere in my script
    else
        logfile=$log
    fi 
	stamp=$(date +"%m/%d/%y %H:%M:%S")
	echo $stamp "$1" >>$logfile
	}


myLOGGER "my log message goes here"

This works perfectly, and does what I expect it to do. Whatever message I put in quotes is passed to the function and appended to the appropriate file.

My question is in whether I can use the command tee to output the same message to the screen and my function?

I know that I can do this:

echo "some text to display and log" | tee -a $log

What I'd like to do is something like the below, but I haven't found a way to do so.

echo "some text I want displayed and logged" | tee (myLOGGER)

I know that I can use tee within my function, however not everything I'm sending to the logs needs to be displayed as well. I suspect that I would just have to add another if statement and pass an additional argument to myLOGGER.

That's the way I'd do it. Accept a "tty too" flag and echo before the remainder of your processing:

if [[ $1 == "-t" ]]
then
    shift 
    echo "$*"
fi

# rest of your log code here

call it like this to write to the tty in addition to the log:

myLOGGER -t "message text"
1 Like

agama,

Took me a moment to decipher what you suggested, but it is perfect and simple. Two things I love in my scripts.

Thanks!!