Index: /trunk/email2trac.py.in
===================================================================
--- /trunk/email2trac.py.in	(revision 409)
+++ /trunk/email2trac.py.in	(revision 410)
@@ -51,7 +51,7 @@
                 -h,--help
                 -f,--file  <configuration file>
-		-n,--dry-run
+                -n,--dry-run
                 -p, --project <project name>
-		-t, --ticket_prefix <name>
+	        -t, --ticket_prefix <name>
 
 SVN Info:
@@ -163,5 +163,5 @@
 	comment = '> '
 
-	def __init__(self, env, parameters, version):
+	def __init__(self, env, parameters, logger, version):
 		self.env = env
 
@@ -173,4 +173,5 @@
 		#
 		self.parameters = parameters
+		self.logger = logger
 
 		# Some useful mail constants
@@ -188,84 +189,12 @@
 		self.get_config = self.env.config.get
 
-		self.setup_log()
+		## init function ##
+		#
 		self.setup_parameters()
 
-
-	def setup_log(self):
-		"""
-		Setup loging
-
-		Note for log format the usage of `$(...)s` instead of `%(...)s` as the latter form
-        would be interpreted by the ConfigParser itself.
-
-		"""
-		if not self.parameters.log_type:
-			self.parameters.log_type = 'syslog'
-
-		if self.parameters.log_type == 'file':
-			if not os.path.isabs(parameters.log_file):
-				self.parameters.log_file = os.path.join(self.env.path, 'log')
-			self.log_handler = logging.FileHandler()
-
-		elif self.parameters.log_type in ('winlog', 'eventlog', 'nteventlog'):
-			# Requires win32 extensions
-			self.log_handler = logging.handlers.NTEventLogHandler(logid, logtype='Application')
-
-		elif self.parameters.log_type in ('syslog', 'unix'):
-			self.log_handler = logging.handlers.SysLogHandler('/dev/log')
-
-		elif self.parameters.log_type in ('stderr'):
-			self.log_handler = logging.StreamHandler(sys.stderr)
-
-		else:
-			self.log_handler = logging.handlers.BufferingHandler(0)
-
-
-		if self.parameters.log_format:
-			self.parameters.log_format = self.parameters.log_format.replace('$(', '%(')
-		else:
-			self.parameters.log_format = 'Email2trac: %(message)s'
-
-		self.logger = logging.getLogger('email2trac')
-
-		## Setup debug level
-		#
-		if not self.parameters.log_level:
-			self.parameters.log_level = 'CRITICAL'
-		else:
-			self.parameters.log_level = self.parameters.log_level.upper()
-
-		if self.parameters.log_level in ['DEBUG', 'ALL'] and self.parameters.debug > 0:
-			self.logger.setLevel(logging.DEBUG)
-
-		elif self.parameters.log_level in ['INFO'] or self.parameters.verbose:
-			self.logger.setLevel(logging.INFO)
-
-		elif self.parameters.log_level in ['WARNING']:
-			self.logger.setLevel(logging.WARNING)
-
-		elif self.parameters.log_level in ['ERROR']:
-			self.logger.setLevel(logging.ERROR)
-
-		elif self.parameters.log_level in ['CRITICAL']:
-			self.logger.setLevel(logging.CRITICAL)
-
-		else:
-			self.logger.setLevel(logging.ERROR)
-
-		self.log_formatter = logging.Formatter(self.parameters.log_format)
-		self.log_handler.setFormatter(self.log_formatter) 
-
-		self.logger.addHandler(self.log_handler)
-			
-		
 	def setup_parameters(self):
 		if self.parameters.has_key('umask'):
 			os.umask(int(self.parameters['umask'], 8))
 
-		if self.parameters.debug:
-			self.parameters.debug = int(self.parameters.debug)
-		else:
-			self.parameters.debug = 0
 
 		if self.parameters.has_key('mailto_link'):
@@ -1063,4 +992,6 @@
 			- trac default values, trac.ini
 		"""
+		self.logger.debug('function set_ticket_fields')
+
 		user_dict = dict()
 
@@ -2179,5 +2110,5 @@
 
 		except Exception, e:
-			print 'TD: Failure sending notification on creation of ticket #%s: %s' %(self.id, e)
+			set.logger.error('Failure sending notification on creation of ticket #%s: %s' %(self.id, e))
 
 
@@ -2217,5 +2148,74 @@
 			project[option] = value 
 
+	## Convert debug value to int
+	#
+	if project.debug:
+		project.debug = int(project.debug)
+	else:
+		project.debug = 0
+
 	return project
+
+########## Setup Logging ###############################################################
+
+def setup_log(parameters):
+	"""
+	Setup loging
+
+		Note for log format the usage of `$(...)s` instead of `%(...)s` as the latter form
+        would be interpreted by the ConfigParser itself.
+
+	"""
+	logger = logging.getLogger('email2trac')
+
+	if not parameters.log_type:
+		parameters.log_type = 'syslog'
+
+	if parameters.log_type == 'file':
+		if not os.path.isabs(parameters.log_file):
+			parameters.log_file = os.path.join('log')
+		log_handler = logging.FileHandler()
+
+	elif parameters.log_type in ('winlog', 'eventlog', 'nteventlog'):
+		# Requires win32 extensions
+		log_handler = logging.handlers.NTEventLogHandler(logid, logtype='Application')
+
+	elif parameters.log_type in ('syslog', 'unix'):
+		log_handler = logging.handlers.SysLogHandler('/dev/log')
+
+	elif parameters.log_type in ('stderr'):
+		log_handler = logging.StreamHandler(sys.stderr)
+
+	else:
+		log_handler = logging.handlers.BufferingHandler(0)
+
+	if parameters.log_format:
+		parameters.log_format = parameters.log_format.replace('$(', '%(')
+	else:
+		parameters.log_format = 'Email2trac: %(message)s'
+
+	log_formatter = logging.Formatter(parameters.log_format)
+	log_handler.setFormatter(log_formatter) 
+	logger.addHandler(log_handler)
+
+	if (parameters.log_level in ['DEBUG', 'ALL']) or (parameters.debug > 0):
+		logger.setLevel(logging.DEBUG)
+
+	elif parameters.log_level in ['INFO'] or parameters.verbose:
+		logger.setLevel(logging.INFO)
+
+	elif parameters.log_level in ['WARNING']:
+		logger.setLevel(logging.WARNING)
+
+	elif parameters.log_level in ['ERROR']:
+		logger.setLevel(logging.ERROR)
+
+	elif parameters.log_level in ['CRITICAL']:
+		logger.setLevel(logging.CRITICAL)
+
+	else:
+		logger.setLevel(logging.INFO)
+
+	return logger
 
 
@@ -2261,4 +2261,13 @@
 	
 	settings = ReadConfig(configfile, project_name)
+
+	# The default prefix for ticket values in email2trac.conf
+	#
+	settings['ticket_prefix'] = ticket_prefix
+	settings['dry_run'] = dry_run
+	settings['verbose'] = verbose
+
+	logger = setup_log(settings)
+
 	if not settings.has_key('project'):
 		print __doc__
@@ -2269,11 +2278,4 @@
 		settings['component'] = component
 
-	# The default prefix for ticket values in email2trac.conf
-	#
-	settings['ticket_prefix'] = ticket_prefix
-	settings['dry_run'] = dry_run
-	settings['verbose'] = verbose
-
-
 	# Determine major trac version used to be in email2trac.conf
 	# Quick hack for 0.12
@@ -2283,6 +2285,5 @@
 		version = '0.12'
 
-	if verbose:
-		print "Found trac version: %s" %(version)
+	logger.debug("Found trac version: %s" %(version))
 	
 	#debug HvB
@@ -2338,5 +2339,5 @@
 
 		else:
-			print 'TRAC version %s is not supported' %version
+			logger.error('TRAC version %s is not supported' %version)
 			sys.exit(1)
 			
@@ -2353,10 +2354,10 @@
 
 	
-		if int(settings['debug']) > 0:
-			print 'Loading environment', settings['project']
+		if settings.debug > 0:
+			logger.debug('Loading environment %s', settings.project)
 
 		env = Environment(settings['project'], create=0)
 
-		tktparser = TicketEmailParser(env, settings, float(version))
+		tktparser = TicketEmailParser(env, settings, logger, float(version))
 		tktparser.parse(sys.stdin)
 
@@ -2379,5 +2380,4 @@
 			tktparser.save_email_for_debug(m, True)
 
-
 		sys.exit(1)
 # EOB
