Skip to content

Instantly share code, notes, and snippets.

@RichardBronosky
Created August 25, 2010 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardBronosky/550332 to your computer and use it in GitHub Desktop.
Save RichardBronosky/550332 to your computer and use it in GitHub Desktop.
# sample output can be found at http://gist.github.com/550332#file_bash.log.sh.log
LOGFILE=/tmp/${0##*/}.log
# a general purpose logging function that is used as prefix similar to time see: man time
log(){
(printf \#%.0s {1..50}; echo)>> $LOGFILE
if [[ $1 == '-s' ]];then
shift
echo -e "$*" >> $LOGFILE
return 0
fi
if [[ -t 0 ]]; then
echo $* >> $LOGFILE
$* 2>&1 | tee -a $LOGFILE
else
$* << EOF 2>&1 | tee -a $LOGFILE
$(echo "$* << FROM_STDIN" >> $LOGFILE; tee -a $LOGFILE; echo "FROM_STDIN" >> $LOGFILE)
EOF
fi
}
# a function for demoing
my_func(){
echo $* to standard out > /dev/stdout
echo to standard error > /dev/stderr
}
# truncate the file
echo -n >$LOGFILE
# log a string
log -s "starting a\nnew log..."
# log a simple command
log date
# log a complex command (`du` prefixed with `time` prefixed with `log`) that will [likely] encounter permissions errors
log time du -sh /etc/
# log a function call with arguments
log my_func alfa bravo charlie
# log a pipe
cat /etc/hosts | log wc
# log a redirect
log wc < /etc/hosts
# log a heredoc
log wc << EOF
Acknowledgments:
Copyright (c) 2010 Richard Bronosky
Offered under the terms of the MIT License.
http://www.opensource.org/licenses/mit-license.php
Created while employed by CMGdigital
EOF
# show the result
echo -e "\n\n########## LOG CONTENTS ##########\n"
cat $LOGFILE
##################################################
starting a
new log...
##################################################
date
Wed Aug 25 23:15:19 EDT 2010
##################################################
time du -sh /etc/
du: /etc//cups/certs: Permission denied
1.1M /etc/
0.00 real 0.00 user 0.00 sys
##################################################
my_func alfa bravo charlie
alfa bravo charlie to standard out
to standard error
##################################################
wc << FROM_STDIN
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
127.0.0.1 signin.example.com
127.0.0.1 www.example.com
127.0.0.1 example.com
FROM_STDIN
8 14 186
##################################################
wc << FROM_STDIN
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
127.0.0.1 signin.example.com
127.0.0.1 www.example.com
127.0.0.1 example.com
FROM_STDIN
8 14 186
##################################################
wc << FROM_STDIN
Acknowledgments:
Copyright (c) 2010 Richard Bronosky
Offered under the terms of the MIT License.
http://www.opensource.org/licenses/mit-license.php
Created while employed by CMGdigital
FROM_STDIN
5 20 201
Wed Aug 25 23:15:19 EDT 2010
du: /etc//cups/certs: Permission denied
1.1M /etc/
0.00 real 0.00 user 0.00 sys
alfa bravo charlie to standard out
to standard error
8 14 186
8 14 186
5 20 201
########## LOG CONTENTS ##########
##################################################
starting a
new log...
##################################################
date
Wed Aug 25 23:15:19 EDT 2010
##################################################
time du -sh /etc/
du: /etc//cups/certs: Permission denied
1.1M /etc/
0.00 real 0.00 user 0.00 sys
##################################################
my_func alfa bravo charlie
alfa bravo charlie to standard out
to standard error
##################################################
wc << FROM_STDIN
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
127.0.0.1 signin.example.com
127.0.0.1 www.example.com
127.0.0.1 example.com
FROM_STDIN
8 14 186
##################################################
wc << FROM_STDIN
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
127.0.0.1 signin.example.com
127.0.0.1 www.example.com
127.0.0.1 example.com
FROM_STDIN
8 14 186
##################################################
wc << FROM_STDIN
Acknowledgments:
Copyright (c) 2010 Richard Bronosky
Offered under the terms of the MIT License.
http://www.opensource.org/licenses/mit-license.php
Created while employed by CMGdigital
FROM_STDIN
5 20 201
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment