(self)
| 6394 | |
| 6395 | # other test methods added below |
| 6396 | def test_rollover(self): |
| 6397 | fh = logging.handlers.TimedRotatingFileHandler( |
| 6398 | self.fn, 'S', encoding="utf-8", backupCount=1) |
| 6399 | fmt = logging.Formatter('%(asctime)s %(message)s') |
| 6400 | fh.setFormatter(fmt) |
| 6401 | r1 = logging.makeLogRecord({'msg': 'testing - initial'}) |
| 6402 | fh.emit(r1) |
| 6403 | self.assertLogFile(self.fn) |
| 6404 | time.sleep(1.1) # a little over a second ... |
| 6405 | r2 = logging.makeLogRecord({'msg': 'testing - after delay'}) |
| 6406 | fh.emit(r2) |
| 6407 | fh.close() |
| 6408 | # At this point, we should have a recent rotated file which we |
| 6409 | # can test for the existence of. However, in practice, on some |
| 6410 | # machines which run really slowly, we don't know how far back |
| 6411 | # in time to go to look for the log file. So, we go back a fair |
| 6412 | # bit, and stop as soon as we see a rotated file. In theory this |
| 6413 | # could of course still fail, but the chances are lower. |
| 6414 | found = False |
| 6415 | now = datetime.datetime.now() |
| 6416 | GO_BACK = 5 * 60 # seconds |
| 6417 | for secs in range(GO_BACK): |
| 6418 | prev = now - datetime.timedelta(seconds=secs) |
| 6419 | fn = self.fn + prev.strftime(".%Y-%m-%d_%H-%M-%S") |
| 6420 | found = os.path.exists(fn) |
| 6421 | if found: |
| 6422 | self.rmfiles.append(fn) |
| 6423 | break |
| 6424 | msg = 'No rotated files found, went back %d seconds' % GO_BACK |
| 6425 | if not found: |
| 6426 | # print additional diagnostics |
| 6427 | dn, fn = os.path.split(self.fn) |
| 6428 | files = [f for f in os.listdir(dn) if f.startswith(fn)] |
| 6429 | print('Test time: %s' % now.strftime("%Y-%m-%d %H-%M-%S"), file=sys.stderr) |
| 6430 | print('The only matching files are: %s' % files, file=sys.stderr) |
| 6431 | for f in files: |
| 6432 | print('Contents of %s:' % f) |
| 6433 | path = os.path.join(dn, f) |
| 6434 | with open(path, 'r') as tf: |
| 6435 | print(tf.read()) |
| 6436 | self.assertTrue(found, msg=msg) |
| 6437 | |
| 6438 | def test_rollover_at_midnight(self, weekly=False): |
| 6439 | os_helper.unlink(self.fn) |
nothing calls this directly
no test coverage detected