Problem with logging module

Steve Erickson steve_erickson at hotmail.com
Wed Oct 13 18:51:16 CEST 2004


I have a logger class that uses the Python logging module.  When I
call it within a program using the unittest module, I get one line in
the log file for the first test, two identical ones for the second,
etc.  I'm using local variables, which should go out of context with
each test.  Setting the 'propagate' property to False doesn't have any
affect.

import logging, os, signal, sys
from datetime import datetime

class Logger:
	def __init__(self, dir, name):
		self.dir = dir
		self.name = name

		self.logger = logging.getLogger(name)
		self.logger.propagate = False
		handler = logging.FileHandler(dir + '/' + name + '.log')
		self.logger.addHandler(handler)
		self.logger.setLevel(logging.INFO)

	def log(self, text):
		msg = datetime.utcnow().isoformat(' ') + ' ' + sys.argv[0] + ' -- '
+ text
		self.logger.info(msg)

import os, sys, unittest
import log

class Test(unittest.TestCase):
	
	def test_1(self):
		homedir = sys.path[0]
		logger = log.Logger(homedir, "test")
		logger.log("Test 1")

	def test_2(self):
		homedir = sys.path[0]
		logger = log.Logger(homedir, "test")
		logger.log("Test 2")
			
	def test_3(self):
		homedir = sys.path[0]
		logger = log.Logger(homedir, "test")
		logger.log("Test 3")
		
	def test_4(self):
		homedir = sys.path[0]
		logger = log.Logger(homedir, "test")
		logger.log("Test 4")
			
	def test_5(self):
		homedir = sys.path[0]
		logger = log.Logger(homedir, "test")
		logger.log("Test 5")

if __name__ == '__main__':
    unittest.main()

And the output file looks like:

2004-10-13 15:54:08.321112 test.py -- Test 1
2004-10-13 15:54:08.326594 test.py -- Test 2
2004-10-13 15:54:08.326594 test.py -- Test 2
2004-10-13 15:54:08.329742 test.py -- Test 3
2004-10-13 15:54:08.329742 test.py -- Test 3
2004-10-13 15:54:08.329742 test.py -- Test 3
2004-10-13 15:54:08.333411 test.py -- Test 4
2004-10-13 15:54:08.333411 test.py -- Test 4
2004-10-13 15:54:08.333411 test.py -- Test 4
2004-10-13 15:54:08.333411 test.py -- Test 4
2004-10-13 15:54:08.336997 test.py -- Test 5
2004-10-13 15:54:08.336997 test.py -- Test 5
2004-10-13 15:54:08.336997 test.py -- Test 5
2004-10-13 15:54:08.336997 test.py -- Test 5
2004-10-13 15:54:08.336997 test.py -- Test 5



More information about the Python-list mailing list