shopt -s expand_aliases
#-t tag, -i pid
alias logfmd='logger -t $0 -i'
logfile="/tmp/stars.log"
logfmd -s "`date "+%Y%m%d %T"` the distance of nearby stars: " 2>$logfile
logfmd -s "`date "+%Y%m%d %T"`>> Sun 0 Sirius 8.6" 2>>$logfile
cat $logfile
-------output----------
#-t tag, -i pid
bash[18996]: 20170322 14:08:33 the distance of nearby stars:
bash[18998]: 20170322 14:08:33>> Sun 0 Sirius 8.6
|
use Log::Log4perl qw(:easy);
Log::Log4perl->easy_init({level=>$DEBUG,
file=>">/tmp/stars.log",
layout=>'%F{1}-%L-%M: %m%n'});
my $logger = get_logger(__PACKAGE__);
%lightyear=(Sun=>0,Sirius=>8.6);
$logger->info("the distance of nearby stars: ");
$logger->debug("--> $s", join(' ',%lightyear));
open FH,"<","/tmp/stars.log";
print while(<FH>);
-------output----------
-e-7-main::: the distance of nearby stars:
-e-8-main::: --> Sun 0 Sirius 8.6
|
import logging
#set the logging output level to debug
logging.basicConfig(level=logging.DEBUG)
#create a filehandle with logging level debug
logger=logging.getLogger(__name__)
handler=logging.FileHandler('/tmp/stars.log','w')
fmt1=logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') #<--
fmt2 = logging.Formatter('%(asctime)s %(levelname)s %(threadName)s: %(message)s','%b %d %H:%M:%S') #<--
handler.setFormatter(fmt2) #<--
logger.addHandler(handler)
lightyear={'Sun':0,'Sirius':8.6}
logger.info('the distance of nearby stars: ')
logger.debug('--> %s' % lightyear)
with open('/tmp/stars.log','r') as f: print f.read()
-------output----------
#set the logging output level to debug
#create a filehandle with logging level debug
Mar 22 14:08:34 INFO MainThread: the distance of nearby stars:
Mar 22 14:08:34 DEBUG MainThread: --> {'Sun': 0, 'Sirius': 8.6}
|