Package yum :: Module logginglevels
[hide private]
[frames] | no frames]

Source Code for Module yum.logginglevels

  1  # This program is free software; you can redistribute it and/or modify 
  2  # it under the terms of the GNU General Public License as published by 
  3  # the Free Software Foundation; either version 2 of the License, or 
  4  # (at your option) any later version. 
  5  # 
  6  # This program is distributed in the hope that it will be useful, 
  7  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
  8  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  9  # GNU Library General Public License for more details. 
 10  # 
 11  # You should have received a copy of the GNU General Public License 
 12  # along with this program; if not, write to the Free Software 
 13  # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
 14   
 15   
 16  """ 
 17  Custom logging levels for finer-grained logging using python's standard 
 18  logging module. 
 19  """ 
 20   
 21  import os 
 22  import socket 
 23  import sys 
 24  import logging 
 25  import logging.handlers 
 26   
 27  INFO_1 = 19 
 28  INFO_2 = 18 
 29   
 30  DEBUG_1 = 9 
 31  DEBUG_2 = 8 
 32  DEBUG_3 = 7 
 33  DEBUG_4 = 6 
 34   
 35  logging.addLevelName(INFO_1, "INFO_1") 
 36  logging.addLevelName(INFO_2, "INFO_2") 
 37   
 38  logging.addLevelName(DEBUG_1, "DEBUG_1") 
 39  logging.addLevelName(DEBUG_2, "DEBUG_2") 
 40  logging.addLevelName(DEBUG_3, "DEBUG_3") 
 41  logging.addLevelName(DEBUG_4, "DEBUG_4") 
 42   
 43  # High level to effectively turn off logging. 
 44  # For compatability with the old logging system. 
 45  __NO_LOGGING = 100 
 46  logging.raiseExceptions = False 
 47   
 48  import syslog as syslog_module 
 49   
 50  syslog = None 
 51   
 52  # Mostly borrowed from original yum-updated.py 
 53  _syslog_facility_map = { "KERN"   : syslog_module.LOG_KERN, 
 54                           "USER"   : syslog_module.LOG_USER, 
 55                           "MAIL"   : syslog_module.LOG_MAIL, 
 56                           "DAEMON" : syslog_module.LOG_DAEMON, 
 57                           "AUTH"   : syslog_module.LOG_AUTH, 
 58                           "LPR"    : syslog_module.LOG_LPR, 
 59                           "NEWS"   : syslog_module.LOG_NEWS, 
 60                           "UUCP"   : syslog_module.LOG_UUCP, 
 61                           "CRON"   : syslog_module.LOG_CRON, 
 62                           "LOCAL0" : syslog_module.LOG_LOCAL0, 
 63                           "LOCAL1" : syslog_module.LOG_LOCAL1, 
 64                           "LOCAL2" : syslog_module.LOG_LOCAL2, 
 65                           "LOCAL3" : syslog_module.LOG_LOCAL3, 
 66                           "LOCAL4" : syslog_module.LOG_LOCAL4, 
 67                           "LOCAL5" : syslog_module.LOG_LOCAL5, 
 68                           "LOCAL6" : syslog_module.LOG_LOCAL6, 
 69                           "LOCAL7" : syslog_module.LOG_LOCAL7,} 
70 -def syslogFacilityMap(facility):
71 if type(facility) == int: 72 return facility 73 elif facility.upper() in _syslog_facility_map: 74 return _syslog_facility_map[facility.upper()] 75 elif (facility.upper().startswith("LOG_") and 76 facility[4:].upper() in _syslog_facility_map): 77 return _syslog_facility_map[facility[4:].upper()] 78 return syslog.LOG_USER
79
80 -def logLevelFromErrorLevel(error_level):
81 """ Convert an old-style error logging level to the new style. """ 82 error_table = { -1 : __NO_LOGGING, 0 : logging.CRITICAL, 1 : logging.ERROR, 83 2 : logging.WARNING} 84 85 return __convertLevel(error_level, error_table)
86
87 -def logLevelFromDebugLevel(debug_level):
88 """ Convert an old-style debug logging level to the new style. """ 89 debug_table = {-1 : __NO_LOGGING, 0 : logging.INFO, 1 : INFO_1, 2 : INFO_2, 90 3 : logging.DEBUG, 4 : DEBUG_1, 5 : DEBUG_2, 6 : DEBUG_3, 7 : DEBUG_4} 91 92 return __convertLevel(debug_level, debug_table)
93
94 -def __convertLevel(level, table):
95 """ Convert yum logging levels using a lookup table. """ 96 # Look up level in the table. 97 try: 98 new_level = table[level] 99 except KeyError: 100 keys = table.keys() 101 # We didn't find the level in the table, check if it's smaller 102 # than the smallest level 103 if level < keys[0]: 104 new_level = table[keys[0]] 105 # Nope. So it must be larger. 106 else: 107 new_level = table[keys[-2]] 108 109 return new_level
110
111 -def setDebugLevel(level):
112 converted_level = logLevelFromDebugLevel(level) 113 logging.getLogger("yum.verbose").setLevel(converted_level)
114
115 -def setErrorLevel(level):
116 converted_level = logLevelFromErrorLevel(level) 117 logging.getLogger("yum").setLevel(converted_level)
118 119 _added_handlers = False
120 -def doLoggingSetup(debuglevel, errorlevel, 121 syslog_ident=None, syslog_facility=None, 122 syslog_device='/dev/log'):
123 """ 124 Configure the python logger. 125 126 errorlevel is optional. If provided, it will override the logging level 127 provided in the logging config file for error messages. 128 debuglevel is optional. If provided, it will override the logging level 129 provided in the logging config file for debug messages. 130 """ 131 global _added_handlers 132 133 logging.basicConfig() 134 135 if _added_handlers: 136 if debuglevel is not None: 137 setDebugLevel(debuglevel) 138 if errorlevel is not None: 139 setErrorLevel(errorlevel) 140 return 141 142 plainformatter = logging.Formatter("%(message)s") 143 syslogformatter = logging.Formatter("yum: %(message)s") 144 145 console_stdout = logging.StreamHandler(sys.stdout) 146 console_stdout.setFormatter(plainformatter) 147 verbose = logging.getLogger("yum.verbose") 148 verbose.propagate = False 149 verbose.addHandler(console_stdout) 150 151 console_stderr = logging.StreamHandler(sys.stderr) 152 console_stderr.setFormatter(plainformatter) 153 logger = logging.getLogger("yum") 154 logger.propagate = False 155 logger.addHandler(console_stderr) 156 157 filelogger = logging.getLogger("yum.filelogging") 158 filelogger.setLevel(logging.INFO) 159 filelogger.propagate = False 160 161 log_dev = syslog_device 162 global syslog 163 if os.path.exists(log_dev): 164 try: 165 syslog = logging.handlers.SysLogHandler(log_dev) 166 syslog.setFormatter(syslogformatter) 167 filelogger.addHandler(syslog) 168 if syslog_ident is not None or syslog_facility is not None: 169 ident = syslog_ident or '' 170 facil = syslog_facility or 'LOG_USER' 171 syslog_module.openlog(ident, 0, syslogFacilityMap(facil)) 172 except socket.error: 173 if syslog is not None: 174 syslog.close() 175 _added_handlers = True 176 177 if debuglevel is not None: 178 setDebugLevel(debuglevel) 179 if errorlevel is not None: 180 setErrorLevel(errorlevel)
181
182 -def setFileLog(uid, logfile):
183 # TODO: When python's logging config parser doesn't blow up 184 # when the user is non-root, put this in the config file. 185 # syslog-style log 186 if uid == 0: 187 try: 188 # For installroot etc. 189 logdir = os.path.dirname(logfile) 190 if not os.path.exists(logdir): 191 os.makedirs(logdir, mode=0755) 192 193 filelogger = logging.getLogger("yum.filelogging") 194 filehandler = logging.FileHandler(logfile) 195 formatter = logging.Formatter("%(asctime)s %(message)s", 196 "%b %d %H:%M:%S") 197 filehandler.setFormatter(formatter) 198 filelogger.addHandler(filehandler) 199 except IOError: 200 logging.getLogger("yum").critical('Cannot open logfile %s', logfile)
201
202 -def setLoggingApp(app):
203 if syslog: 204 syslogformatter = logging.Formatter("yum(%s): "% (app,) + "%(message)s") 205 syslog.setFormatter(syslogformatter)
206