line duplication using logging to file

seb sebastien.thur at laposte.net
Tue Jan 9 09:06:15 CET 2007


Hi, I am writing to a file some basic information using the logging
module.  It is working but in the log file some line are printed
several time. I had put some print debugging messages in the logging
function (so they appear on the consile) and they are called once only.
Obviously there is some understantding of the logging module that I am
missing.

My simple logging program (see below) is called by several processes.
In this way I can collect the information from various sources (and not
use the network enabled logging module)

I am using python 2.4 on WinXP SP2.

Do you have any idea ? Thanks in advance.

Seb.

*****************************************
The very simple "logging program ":
*****************************************
import logging, logging.handlers
import time

def write_log(level, message):
	# Utilisation de l'API pour le Handler global
	print "time.asctime()",time.asctime(),"received level=",level,"message
=",message
	nom_logger="main_log_file"
	logger=logging.getLogger(nom_logger)
	logger.setLevel(logging.DEBUG)
	prefix = "pix_main_log_file"
	#fh=logging.handlers.RotatingFileHandler(prefix + "_log.txt", 'a',
1000000,10)
	fh=logging.FileHandler("main_log.txt")
	fh.setLevel(logging.DEBUG)
	#formater = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s
- %(message)s")
	formater = logging.Formatter("%(asctime)s -  %(levelname)s -
%(message)s")
	fh.setFormatter(formater)
	logger.addHandler(fh)
	#logger.info("***************************** debut")


	#message = main(url,conf_file,logger)
	#message="hello seb"
	if str(level).lower() == "info" :
		print "logger info"
		logger.info(str(message))
	elif str(level).lower() =="error":
		print "logger error"
		logger.error(str(message))
	elif str(level).lower()=="warning" :
		print "logger warning"
		logger.warning(str(message))
	elif str(level).lower() =="critical":
		print "logger critical"
		logger.critical(str(message))
	elif str(level).lower() == "exception":
		print "logger exception"
		logger.exception(str(message))
	else :
		logger.info("niveau inconnu "+str(message))

	print "_________",message
	#print dir(logger)
	return

*************************************************
Example of the log file with duplicated line.
**************************************************

2007-01-08 18:26:19,578 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version
simple\thread_RS232.py___init_rs232initrs232_openCOM1
2007-01-08 18:26:19,578 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version
simple\thread_RS232.py___run____thread lance
2007-01-08 18:26:19,578 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version
simple\thread_RS232.py___run____thread lance
2007-01-08 18:26:32,015 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-08 18:26:32,015 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-08 18:26:32,015 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-08 18:26:42,483 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-08 18:26:42,483 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-08 18:26:42,483 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-08 18:26:42,483 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-08 18:26:53,750 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-08 18:26:53,750 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-08 18:26:53,750 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-08 18:26:53,750 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-08 18:26:53,750 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-08 18:27:03,092 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-08 18:27:03,092 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-08 18:27:03,092 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-08 18:27:03,092 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-08 18:27:03,092 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-08 18:27:03,092 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-08 18:27:13,671 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-08 18:27:13,671 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-08 18:27:13,671 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-08 18:27:13,671 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-08 18:27:13,671 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-08 18:27:13,671 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-08 18:27:13,671 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-08 18:27:14,796 -  WARNING - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___run___fin
dans le run car continue = 0
2007-01-08 18:27:14,796 -  WARNING - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___run___fin
dans le run car continue = 0
2007-01-08 18:27:14,796 -  WARNING - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___run___fin
dans le run car continue = 0
2007-01-08 18:27:14,796 -  WARNING - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___run___fin
dans le run car continue = 0
2007-01-08 18:27:14,796 -  WARNING - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___run___fin
dans le run car continue = 0
2007-01-08 18:27:14,796 -  WARNING - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___run___fin
dans le run car continue = 0
2007-01-08 18:27:14,796 -  WARNING - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___run___fin
dans le run car continue = 0
2007-01-08 18:27:14,796 -  WARNING - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___run___fin
dans le run car continue = 0
2007-01-08 18:27:14,890 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___stopthread
demande d'arret
2007-01-08 18:27:14,890 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___stopthread
demande d'arret
2007-01-08 18:27:14,890 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___stopthread
demande d'arret
2007-01-08 18:27:14,890 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___stopthread
demande d'arret
2007-01-08 18:27:14,890 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___stopthread
demande d'arret
2007-01-08 18:27:14,890 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___stopthread
demande d'arret
2007-01-08 18:27:14,890 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___stopthread
demande d'arret
2007-01-08 18:27:14,890 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___stopthread
demande d'arret
2007-01-08 18:27:14,890 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___stopthread
demande d'arret
2007-01-09 08:51:26,562 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version
simple\thread_RS232.py___init_rs232initrs232_openCOM1
2007-01-09 08:51:26,733 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version
simple\thread_RS232.py___run____thread lance
2007-01-09 08:51:26,733 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version
simple\thread_RS232.py___run____thread lance
2007-01-09 08:51:39,453 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-09 08:51:39,453 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-09 08:51:39,453 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-09 08:51:48,280 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-09 08:51:48,280 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-09 08:51:48,280 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-09 08:51:48,280 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-09 08:51:58,750 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-09 08:51:58,750 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-09 08:51:58,750 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-09 08:51:58,750 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-09 08:51:58,750 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-09 08:52:09,812 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-09 08:52:09,812 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-09 08:52:09,812 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-09 08:52:09,812 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-09 08:52:09,812 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-09 08:52:09,812 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-09 08:52:19,078 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-09 08:52:19,078 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-09 08:52:19,078 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-09 08:52:19,078 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-09 08:52:19,078 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-09 08:52:19,078 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-09 08:52:19,078 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\Client_tests.py___test1TEST
1 = OK
2007-01-09 08:52:22,078 -  WARNING - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___run___fin
dans le run car continue = 0
2007-01-09 08:52:22,078 -  WARNING - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___run___fin
dans le run car continue = 0
2007-01-09 08:52:22,078 -  WARNING - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___run___fin
dans le run car continue = 0
2007-01-09 08:52:22,078 -  WARNING - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___run___fin
dans le run car continue = 0
2007-01-09 08:52:22,078 -  WARNING - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___run___fin
dans le run car continue = 0
2007-01-09 08:52:22,078 -  WARNING - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___run___fin
dans le run car continue = 0
2007-01-09 08:52:22,078 -  WARNING - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___run___fin
dans le run car continue = 0
2007-01-09 08:52:22,078 -  WARNING - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___run___fin
dans le run car continue = 0
2007-01-09 08:52:22,125 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___stopthread
demande d'arret
2007-01-09 08:52:22,125 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___stopthread
demande d'arret
2007-01-09 08:52:22,125 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___stopthread
demande d'arret
2007-01-09 08:52:22,125 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___stopthread
demande d'arret
2007-01-09 08:52:22,125 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___stopthread
demande d'arret
2007-01-09 08:52:22,125 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___stopthread
demande d'arret
2007-01-09 08:52:22,125 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___stopthread
demande d'arret
2007-01-09 08:52:22,125 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___stopthread
demande d'arret
2007-01-09 08:52:22,125 -  INFO - C:\Documents and
Settings\test\Bureau\xmlrpc\version simple\thread_RS232.py___stopthread
demande d'arret


**************************
Program calling the log :
***************************
import appel_log
.....

message = "something"
nom_function_actuelle= str(sys._getframe().f_code.co_filename)
+"___"+str(sys._getframe().f_code.co_name)
appel_log.write_log("info",nom_function_actuelle+message)
................




More information about the Python-list mailing list