Package screenlets :: Module logger
[hide private]
[frames] | no frames]

Source Code for Module screenlets.logger

 1  # This application is released under the GNU General Public License  
 2  # v3 (or, at your option, any later version). You can find the full  
 3  # text of the license under http://www.gnu.org/licenses/gpl.txt.  
 4  # By using, editing and/or distributing this software you agree to  
 5  # the terms and conditions of this license.  
 6  # Thank you for using free software! 
 7   
 8  # Logger and command line parser for universal-applets. 
 9  # (c) 2008 Przemyslaw Firszt (pefi) <pefi@epf.pl> 
10   
11  import os 
12  import sys 
13  import logging 
14   
15  import screenlets 
16   
17 -def logger_init():
18 """Initialize logger""" 19 options = screenlets.COMMAND_LINE_OPTIONS 20 #create main logger 21 log = logging.getLogger(screenlets.LOG_NAME) 22 if not options.LOG_DISABLED: 23 #set log output 24 if options.LOG_OUTPUT == "STDOUT": 25 log_file = logging.StreamHandler(sys.stdout) 26 elif options.LOG_OUTPUT == "STDERR": 27 log_file = logging.StreamHandler(sys.stderr) 28 elif options.LOG_OUTPUT == "FILE": 29 try: 30 log_file = logging.FileHandler(screenlets.LOG_FILE, "w") 31 except IOError: 32 print("Cannot create %s logfile. Using STDERR instead.") %(screenlets.LOG_FILE) 33 log_file = logging.StreamHandler(sys.stderr) 34 else: 35 print("Unknown output type: %s, using STDERR instead.") %(screenlets.LOG_FILE) 36 log_file = logging.StreamHandler(sys.stderr) 37 #check if LOG_LEVEL is valid and set 38 try: 39 level = 51 - (10 * int(options.LOG_LEVEL)) #multiply by 10 and substract from 51 to fit into allowed range 40 log.setLevel(level) 41 log_file.setLevel(level) 42 except ValueError: 43 print ("LOG_LEVEL %s is not allowed. Use -h to get more information. Using LOG_LEVEL=5") %(screenlets.LOG_LEVEL) 44 log.setLevel(1) #command line paramete was wrong, but user wanted logging ..."1" means log everything 45 log_file.setLevel(1) 46 else: 47 #do not log anything but we still have to provide logger to avoid errors 48 log_file = logging.StreamHandler(sys.stderr) 49 #screenlets.LOG_LEVEL = 51 50 log.setLevel(51) 51 log_file.setLevel(51) 52 53 #set the format of log message 54 log_formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s") 55 log_file.setFormatter(log_formatter) 56 57 #add file to logger 58 log.addHandler(log_file) 59 return log
60 61 log = logger_init() 62
63 -def get_default_logger():
64 """This function returns default logger""" 65 global log 66 # Try to return the logger (an exception mean that the logger has not yet been initialized) 67 try: 68 return log 69 except NameError: 70 log = logger_init() 71 if log: 72 log.debug("%s: Logger initialized" %__name__) 73 return log 74 else: 75 print("%s: Cannot initialize logger!" %__name__) #TODO to quit or not to quit? Lack of logger may cause random errors 76 return None #TODO Raise Exception?
77