Changeset 270
- Timestamp:
- 09/15/2010 04:37:08 PM (12 years ago)
- Location:
- SHX/trunk/src/SeismicHandler
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
SHX/trunk/src/SeismicHandler/modules/log.py
r258 r270 23 23 24 24 from SeismicHandler.basics import Singleton 25 from SeismicHandler.modules.runtime import runtime 25 26 import logging 26 27 import os 27 28 import atexit 29 import warnings 28 30 29 31 class Logging(object): … … 32 34 33 35 def __init__(self): 36 if not self.logging: 37 self.setup() 38 39 def setup(self): 34 40 """configure logging once""" 35 41 36 # logging.Logger.__init__(self, "shx") 42 # get log level 43 level_file = runtime().config.logging.level_file[0] 44 level_screen = runtime().config.logging.level_screen[0] 37 45 38 if not self.logging: 39 # get temp directory for logfile 40 try: 41 # mostly set on windows 42 dir = os.environ["TEMP"] 43 except KeyError: 44 # assume linux system 45 dir = "/tmp/" 46 # get temp directory for logfile 47 try: 48 # mostly set on windows 49 directory = os.environ["TEMP"] 50 except KeyError: 51 # assume linux system 52 directory = "/tmp/" 46 53 47 48 49 50 self.logdest = os.path.join(dir, logfile)54 # recent logfile depends on pid - concurrently running processes get 55 # a new logfile each 56 logfile = 'sh-%u.log' % os.getpid() 57 self.logdest = os.path.join(directory, logfile) 51 58 52 53 54 self.backupdest = os.path.join(dir, backupfile)59 # backup location depends on user id (just to avoid permission issues) 60 backupfile = 'shbckp-%s.log' % os.getuid() 61 self.backupdest = os.path.join(directory, backupfile) 55 62 56 57 58 level=logging.DEBUG,59 60 61 62 63 64 65 66 "Logging everything to console." % dir)63 try: 64 logging.basicConfig( 65 level=getattr(logging, level_file.upper()), 66 format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', 67 datefmt='%Y-%m-%d %H:%M:%S', 68 filename=self.logdest, 69 filemode='w' 70 ) 71 except IOError: 72 logging.warning("Cannot log to disc, path %s not writable! " 73 "Logging everything to console." % directory) 67 74 68 69 level=logging.DEBUG,70 71 72 73 74 75 76 console.setLevel(logging.ERROR)77 75 logging.basicConfig( 76 level=getattr(logging, level_file.upper()), 77 format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', 78 datefmt='%Y-%m-%d %H:%M:%S' 79 ) 80 else: 81 # additional to console 82 console = logging.StreamHandler() 83 console.setLevel(getattr(logging, level_screen.upper())) 84 #console.setLevel(logging.DEBUG) 78 85 79 80 81 86 # set a format which is simpler for console use 87 formatter = \ 88 logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') 82 89 83 84 90 # tell the handler to use this format 91 console.setFormatter(formatter) 85 92 86 87 93 # add the handler to the root logger 94 logging.getLogger('').addHandler(console) 88 95 89 96 self.logging = logging 90 97 91 98 atexit.register(self.shutdown) 92 99 93 100 def shutdown(self): … … 100 107 os.rename(self.logdest, self.backupdest) 101 108 except Exception, e: 102 pass 103 # print e 109 warnings.warn("Error while shutting down logging: %s" % e) 104 110 105 111 def newLogger(self, name): … … 110 116 return logging.getLogger(name) 111 117 112 # init logging once (sets logfile, etc) 113 Logging() 118 if __name__ == "__main__": 119 a = Logging().newLogger("test") 120 a.info("info") 121 a.error("error") 122 a.debug("debug") -
SHX/trunk/src/SeismicHandler/modules/runtime.py
r269 r270 22 22 """ 23 23 24 # Please note that in this file no logging takes place since configuration 25 # options for logging are initialized here first. 26 24 27 import sys 25 28 import os … … 40 43 41 44 Now a file named TEST should contain 42 >>> open("TEST").read() [:34]43 "<open file 'TEST', mode 'ab' at 0x "45 >>> open("TEST").read() #doctest: +ELLIPSIS 46 "<open file 'TEST', mode 'ab' at 0x..." 44 47 45 48 This is only for clean-up: … … 53 56 54 57 # configuration is read from file 55 __configuration= None58 config = None 56 59 57 60 # global switches (see http://www.seismic-handler.org/portal/wiki/ShSwitch) … … 79 82 80 83 def __init__(self): 81 if not self. __configuration:84 if not self.config: 82 85 self.readConfig() 83 86 … … 167 170 # check for variable definition 168 171 if i[0].startswith("$"): 169 vars[i[0].lower()] = __handlePlaceholdersVars(i[1]) 172 v = i[0].lower() 173 if v in vars: 174 warnings.warn("Variable '%s' defined more " 175 "than once!" % v, SyntaxWarning) 176 177 vars[v] = __handlePlaceholdersVars(i[1]) 170 178 else: 171 179 conf[s][i[0]] = __handlePlaceholdersVars(i[1]) 172 180 173 # check for include statement181 # check if include statement was defined -> process last 174 182 if "include" in conf[s]: 175 183 … … 181 189 # only include current section 182 190 if not extra.has_section(s): 191 warnings.warn("To be included configuration file " 192 "'%s' not found." % e, SyntaxWarning) 183 193 break 184 194 … … 196 206 conf[s][i] = __handlePlaceholdersVars(conf[s][i], True) 197 207 198 self. __configuration= conf208 self.config = conf 199 209 self.__fullconfig = fullconfig 200 210 -
SHX/trunk/src/SeismicHandler/shx.conf
r268 r270 14 14 stations = [SHX]/stations/,[PRIVATE]/stations/,/mnt/stations/ 15 15 16 [logging] 17 # possible values: debug, info, error, XXX 18 level_file = debug 19 level_screen = error 20 16 21 [miniseed] 17 backend = szgrf 22 backend = bgrszo,sfd 23 24 [bgrszo] 25 host = 192.168.11.151 26 db = sfdb 27 use_credentials = no 28 user = 29 password = 30 31 [sfd] 32 default_path = . 18 33 19 34 [graphics] … … 28 43 # If the included configuration file is not found, the error will be 29 44 # passed silently. 45 46 [layout] 47 # You are able to define variables. All definitions are globally no 48 # matter where they are defined. Substitution will be done as last step 49 # while processing the configuration file. 50 $grsn = WET,FUR,BSEG 51 $grf = GRA1 52 read01 = MOX,$grsn,$grf
Note: See TracChangeset
for help on using the changeset viewer.