Coding Bash vs. Perl vs. Python (by Evan Liu)


Topics about logging

Logging: logging to console


		  
		  

Tasks:

		  
Bash Perl Python
  1. #-s output message to standard error as well
  2. logger -s "the distance of nearby stars: " 2>&1
  3. logger -s ">> Sun 0 Sirius8.6" 2>&1
  4. -------output----------
  1. #-s output message to standard error as well
  2. neutrino0717: the distance of nearby stars:
  3. neutrino0717: >> Sun 0 Sirius8.6
  1. #get_logger() is equivalent to get_logger(__PACKAGE__).
  2. #get_logger('') will obtain the root logger
  3. use Log::Log4perl qw(:easy);
  4. Log::Log4perl->easy_init($INFO); # Set priority of root logger to INFO
  5. my $logger = get_logger();
  6. %lightyear=(Sun=>0,Sirius=>8.6);
  7. $logger->info("the distance of nearby stars: ");
  8. $logger->debug("--> $s", join(' ',%lightyear));
  9. -------output----------
  1. #get_logger() is equivalent to get_logger(__PACKAGE__).
  2. #get_logger('') will obtain the root logger
  1. import sys
  2. import logging
  3. #set the logging output level to debug
  4. #by default, sys.stderr will be used.
  5. logging.basicConfig(level=logging.INFO,stream=sys.stdout)
  6. #hierarchical,getLogger() will return root logger
  7. logger=logging.getLogger(__name__)
  8. lightyear={'Sun':0,'Sirius':8.6}
  9. logger.info('the distance of nearby stars: ')
  10. logger.debug('--> %s' % lightyear)
  11. -------output----------
  1. #set the logging output level to debug
  2. #by default, sys.stderr will be used.
  3. #hierarchical,getLogger() will return root logger
  4. INFO:__main__:the distance of nearby stars:

Logging: logging to a logfile with a FileHandler


		  
		  

Tasks:

		  
Bash Perl Python
  1. logfile="/tmp/stars.log"
  2. logger -s "the distance of nearby stars: " 2>$logfile
  3. logger -s ">> Sun 0 Sirius 8.6" 2>>$logfile
  4. cat $logfile
  5. -------output----------
  1. neutrino0717: the distance of nearby stars:
  2. neutrino0717: >> Sun 0 Sirius 8.6
  1. use Log::Log4perl qw(:easy);
  2. Log::Log4perl->easy_init({
  3. level=>$DEBUG,
  4. file=>">/tmp/stars.log"});
  5. my $logger = get_logger(__PACKAGE__);
  6. %lightyear=(Sun=>0,Sirius=>8.6);
  7. $logger->info("the distance of nearby stars: ");
  8. $logger->debug("--> $s", join(' ',%lightyear));
  9. open FH,"<","/tmp/stars.log";
  10. print while(<FH>);
  11. -------output----------
  1. 2017/03/22 14:08:33 the distance of nearby stars:
  2. 2017/03/22 14:08:33 --> Sun 0 Sirius 8.6
  1. import logging
  2. #set the logging output level to debug
  3. logging.basicConfig(level=logging.DEBUG)
  4. logger=logging.getLogger(__name__)
  5. #create a filehandle with logging level debug
  6. handler=logging.FileHandler('/tmp/stars.log','w') #<--
  7. logger.addHandler(handler) #<--
  8. lightyear = {'Sun':0,'Sirius':8.6}
  9. logger.info('the distance of nearby stars: ')
  10. logger.debug('--> %s' % lightyear)
  11. with open('/tmp/stars.log', 'r') as f: print f.read()
  12. -------output----------
  1. #set the logging output level to debug
  2. #create a filehandle with logging level debug
  3. the distance of nearby stars:
  4. --> {'Sun': 0, 'Sirius': 8.6}

Logging: logging to a logfile with a formated FileHandler


		  
		  

Tasks:

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

Logging: logging to syslog


		  
		  

Tasks:

		  
Bash Perl Python
  1. logger "message from logger command"
  2. tail -n 1 /var/log/syslog
  3. -------output----------
  1. Mar 22 14:08:33 cloud bash[18998]: 20170322 14:08:33>> Sun 0 Sirius 8.6
  1. #todo
  2. -------output----------
  1. #todo
  1. #todo
  2. -------output----------
  1. #todo