I have this pydev project which I did in order to learn how to properly use the logger
project
..|.src
..|..|.core
..|..|...|__init__.py
..|..|...|classHanger.py
..|..|...|scripts
..|..|.entrypoint.py
..|..|.util.py
..|
..|.cli
..|..|.cliloggertest.py
..|
..|.config
.....|.logger.conf
classHangar.py
#!/usr/bin/env python
# --*-- encoding: iso-8859-1 --*--
import logging
class SpeakingClass(object):
def __init__(self):
self.logger = logging.getLogger("%s.%s" % (__name__, "SpeakingClass"))
def speakingMethod(self):
self.logger.info("I'm a method from a SpeakingClass instance")
scripts.py
#!env python
# --*-- encoding: iso-8859-1 --*--
import logging
logger = logging.getLogger("%s.%s" % (__name__, "scripts"))
def anotherRandomMethod():
logger.info("Now I'm talking from core.scripts.anotherRandomMethod")
entrypoint.py
#!/usr/bin/env python
# --*-- encoding: iso-8859-1 --*--
import logging
from core.classHangar import SpeakingClass
from core.scripts import anotherRandomMethod
logger = logging.getLogger("%s.%s" % (__name__, "entrypoint"))
def randomMethod():
logger.info("Now I'm in the entrypoint.randomMethod")
def methodCalledByCli():
logger.info("Now I'm in the entrypoint.methodCalledByCli")
randomMethod()
anotherRandomMethod()
speaking_object = SpeakingClass()
speaking_object.speakingMethod()
cliloggertest.py
#!env python
# --*-- encoding: iso-8859-1 --*--
import sys
sys.path.insert(0, '../src/')
import os
import logging.config
import util
from entrypoint import methodCalledByCli
def main():
logging.config.fileConfig(os.path.join(util.getProjectPath(), "config/logger.conf"))
logger = logging.getLogger("%s.%s" % (__name__, "cli"))
logger.info("I'm talking from the CLI script")
return methodCalledByCli()
if __name__ == "__main__":
sys.exit(main())
and logger.conf
[loggers]
keys=root
[handlers]
keys=syserr
[formatters]
keys=basicformatter
[logger_root]
level=DEBUG
handlers=syserr
[handler_syserr]
class=StreamHandler
formatter=basicformatter
args=(sys.stderr,)
[formatter_basicformatter]
format=%(asctime)s %(levelname)-9s%(name)-35s: %(message)s
datefmt=
I should normally get :
"I'm talking from the CLI script"
"Now I'm in the entrypoint.methodCalledByCli"
"Now I'm in the entrypoint.randomMethod"
"Now I'm talking from core.scripts.anotherRandomMethod"
"I'm a method from a SpeakingClass instance"
but all I'm getting is :
"I'm talking from the CLI script"
"I'm a method from a SpeakingClass instance"
I don't see why, I requested the logger from logging exactly the same way in cliloggertest.py and the other scripts
ps : I'm lauching cliloggertest.py
EDIT
As stated by Mikko Ohtamaa it's because of the import order, cli was importing entrypoint and entrypoint is importing scripts, thus script's logger and entrypoint's logger are created BEFORE setting the configuration in CLI
Changing cliloggertest this way solves the problem (set the configuration before ANY non internal python import :
#!env python
# --*-- encoding: iso-8859-1 --*--
import sys
sys.path.insert(0, '../src/')
import os
import util
import logging.config
logging.config.fileConfig(os.path.join(util.getProjectPath(), "config/logger.conf"))
from entrypoint import methodCalledByCli
def main():
logger = logging.getLogger("cliloggertest")
logger.info("I'm talking from the CLI script")
return methodCalledByCli()
if __name__ == "__main__":
sys.exit(main())
May guess is that this is an order of imports issue and how your application sets up itself.
Some loggers get created before
logging.config.fileConfig()
call, others after the call.