A Logfile class with different policies for file creation
| 24 | # ipython and does input cache management. Finish cleanup later... |
| 25 | |
| 26 | class Logger(object): |
| 27 | """A Logfile class with different policies for file creation""" |
| 28 | |
| 29 | def __init__(self, home_dir, logfname='Logger.log', loghead=u'', |
| 30 | logmode='over'): |
| 31 | |
| 32 | # this is the full ipython instance, we need some attributes from it |
| 33 | # which won't exist until later. What a mess, clean up later... |
| 34 | self.home_dir = home_dir |
| 35 | |
| 36 | self.logfname = logfname |
| 37 | self.loghead = loghead |
| 38 | self.logmode = logmode |
| 39 | self.logfile = None |
| 40 | |
| 41 | # Whether to log raw or processed input |
| 42 | self.log_raw_input = False |
| 43 | |
| 44 | # whether to also log output |
| 45 | self.log_output = False |
| 46 | |
| 47 | # whether to put timestamps before each log entry |
| 48 | self.timestamp = False |
| 49 | |
| 50 | # activity control flags |
| 51 | self.log_active = False |
| 52 | |
| 53 | # logmode is a validated property |
| 54 | def _set_mode(self,mode): |
| 55 | if mode not in ['append','backup','global','over','rotate']: |
| 56 | raise ValueError('invalid log mode %s given' % mode) |
| 57 | self._logmode = mode |
| 58 | |
| 59 | def _get_mode(self): |
| 60 | return self._logmode |
| 61 | |
| 62 | logmode = property(_get_mode,_set_mode) |
| 63 | |
| 64 | def logstart(self, logfname=None, loghead=None, logmode=None, |
| 65 | log_output=False, timestamp=False, log_raw_input=False): |
| 66 | """Generate a new log-file with a default header. |
| 67 | |
| 68 | Raises RuntimeError if the log has already been started""" |
| 69 | |
| 70 | if self.logfile is not None: |
| 71 | raise RuntimeError('Log file is already active: %s' % |
| 72 | self.logfname) |
| 73 | |
| 74 | # The parameters can override constructor defaults |
| 75 | if logfname is not None: self.logfname = logfname |
| 76 | if loghead is not None: self.loghead = loghead |
| 77 | if logmode is not None: self.logmode = logmode |
| 78 | |
| 79 | # Parameters not part of the constructor |
| 80 | self.timestamp = timestamp |
| 81 | self.log_output = log_output |
| 82 | self.log_raw_input = log_raw_input |
| 83 |