@staticmethods called more than once
John Gordon
gordon at panix.com
Tue May 21 12:34:46 EDT 2013
In <02f0123d-2f9e-4287-b983-cfa1db9db69c at googlegroups.com> Christian <mining.facts at gmail.com> writes:
> Hi,
> i'm somewhat confused working with @staticmethods. My logger and configuration methods are called n times, but I have only one call.
> n is number of classes which import the loger and configuration class
> in the subfolder mymodule. What might be my mistake mistake?
> Many thanks
> Christian
> ### __init__.py ###
> from mymodule.MyLogger import MyLogger
> from mymodule.MyConfig import MyConfig
> ##### my_test.py ##########
> from mymodule import MyConfig,MyLogger
> #Both methods are static
> key,logfile,loglevel = MyConfig().get_config('Logging')
> log = MyLogger.set_logger(key,logfile,loglevel)
> log.critical(time.time())
> #Output
> 2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19
> 2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19
> 2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19
> 2013-05-21 17:20:37,192 - my_test - 17 - CRITICAL - 1369149637.19
You haven't given us the code for your MyLogger class, so it's difficult
to say exactly what the problem is.
However, I have a guess. Does MyLogger.set_logger() contain a call to
addHandler()? Each call to addHandler() adds another handler to your
logger, and when you call log.critical() [or any other log function] you
get one line of output for each handler.
You should only call addHandler() once.
--
John Gordon A is for Amy, who fell down the stairs
gordon at panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
More information about the Python-list
mailing list