Package gromacs :: Module log'
[hide private]
[frames] | no frames]

Source Code for Module gromacs.log'

 1  # log.py 
 2  # logging for Gromacs module 
 3  # Copyright (c) 2010 Oliver Beckstein 
 4  # Published under the GNU Public Licence v3 
 5   
 6  """ 
 7  :mod:`gromacs.log` -- setting up logging 
 8  ======================================== 
 9   
10  Configure logging for gromacs. Import this module if logging is 
11  desired in application code. 
12   
13   
14  Logging to a file and the console. 
15   
16  See http://docs.python.org/library/logging.html?#logging-to-multiple-destinations 
17   
18  The top level logger of the library is named 'gromacs'.  Note that we 
19  are configuring this logger with console output. If the root logger 
20  also does this then we will get two output lines to the console. We'll 
21  live with this because this is a simple convenience library and most 
22  people will not bother with a logger (I think...) 
23   
24  In modules that use loggers get a logger like so:: 
25       import logging 
26       logger = logging.getLogger('gromacs.MODULENAME') 
27   
28  """ 
29   
30  import logging 
31   
32 -def create(logger_name, logfile='edPDB.log'):
33 """Create a top level logger. 34 35 - The file logger logs everything (including DEBUG). 36 - The console logger only logs INFO and above. 37 38 Logging to a file and the console. 39 40 See http://docs.python.org/library/logging.html?#logging-to-multiple-destinations 41 42 The top level logger of the library is named 'edPDB'. Note that 43 we are configuring this logger with console output. If the root 44 logger also does this then we will get two output lines to the 45 console. We'll live with this because this is a simple 46 convenience library... 47 """ 48 49 logger = logging.getLogger(logger_name) 50 51 logger.setLevel(logging.DEBUG) 52 53 logfile = logging.FileHandler(logfile) 54 logfile_formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s') 55 logfile.setFormatter(logfile_formatter) 56 logger.addHandler(logfile) 57 58 # define a Handler which writes INFO messages or higher to the sys.stderr 59 console = logging.StreamHandler() 60 console.setLevel(logging.INFO) 61 # set a format which is simpler for console use 62 formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') 63 console.setFormatter(formatter) 64 # configure for Mhp1 only 65 logger.addHandler(console) 66 67 return logger
68
69 -def clear_handlers(logger):
70 """clean out handlers in the library top level logger 71 72 (only important for reload/debug cycles...) 73 """ 74 for h in logger.handlers: 75 logger.removeHandler(h)
76 77
78 -class NullHandler(logging.Handler):
79 """Silent Handler. 80 81 Useful as a default:: 82 h = NullHandler() 83 logging.getLogger("gromacs").addHandler(h) 84 del h 85 """
86 - def emit(self, record):
87 pass
88