Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging ancestors ignored after configuration #46949

Closed
acooke mannequin opened this issue Apr 26, 2008 · 4 comments
Closed

Logging ancestors ignored after configuration #46949

acooke mannequin opened this issue Apr 26, 2008 · 4 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@acooke
Copy link
Mannequin

acooke mannequin commented Apr 26, 2008

BPO 2697
Nosy @vsajip

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/vsajip'
closed_at = <Date 2008-04-28.13:15:53.385>
created_at = <Date 2008-04-26.13:46:36.677>
labels = ['invalid', 'type-bug', 'library']
title = 'Logging ancestors ignored after configuration'
updated_at = <Date 2008-05-06.01:41:29.069>
user = 'https://bugs.python.org/acooke'

bugs.python.org fields:

activity = <Date 2008-05-06.01:41:29.069>
actor = 'acooke'
assignee = 'vinay.sajip'
closed = True
closed_date = <Date 2008-04-28.13:15:53.385>
closer = 'vinay.sajip'
components = ['Library (Lib)']
creation = <Date 2008-04-26.13:46:36.677>
creator = 'acooke'
dependencies = []
files = []
hgrepos = []
issue_num = 2697
keywords = []
message_count = 4.0
messages = ['65843', '65915', '66265', '66300']
nosy_count = 3.0
nosy_names = ['vinay.sajip', 'acooke', 'sayap']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue2697'
versions = ['Python 2.5']

@acooke
Copy link
Mannequin Author

acooke mannequin commented Apr 26, 2008

I am seeing some odd behaviour with logging which would be explained
if loggers that are not defined explicitly (but which are controlled
via their ancestors) must be created after the logging system is
configured via fileConfig().

That's a bit abstract, so here's the problem itself: I define my log
within a module by doing

import logging
log = logging.getLogger(__name__)

Now typically __name__ will be something like "acooke.utils.foo".

That happens before the application configures logging, which it does
by calling logging.config.fileConfig() to load a configuration.

If I do that, then I don't see any logging output from
"acooke.utils.foo" (when using "log" from above after "fileConfig" has
been called) unless I explicitly define a logger with that name.
Neither root nor an "acooke" logger, defined in the config file, are
called.

One way to handle this is to make creation of module-level Loggers
lazy, and make sure that logging initialisation occurs before any
other logging is actually used (which is not so hard - just init log
at the start of the application).

Of course, there's a performance hit...

For example:

class Log(object):
    def __init__(self, name):
        super(Log, self).__init__()
        self._name = name
        self._lazy = None
    def __getattr__(self, key):
        if not self._lazy:
            self._lazy = logging.getLogger(self._name)
        return getattr(self._lazy, key)

and then, in some module:

from acooke.util.log import Log
log = Log(__name__)
[...]
class Foo(object):
    def my_method(self):
        log.debug("this works as expected")

@acooke acooke mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Apr 26, 2008
@vsajip
Copy link
Member

vsajip commented Apr 28, 2008

This is not a bug - it's by design, as I explained in my reply to your
posting on comp.lang.python. In my reply, I suggested how you could
avoid problems.

@vsajip vsajip closed this as completed Apr 28, 2008
@vsajip vsajip added the invalid label Apr 28, 2008
@vsajip vsajip self-assigned this Apr 28, 2008
@sayap
Copy link
Mannequin

sayap mannequin commented May 5, 2008

I'd think that it is a rather common situation where one wants to use
use both fileConfig and module-level logger. As of now, the workaround
suggested by Andrew seems to be the only viable way to achieve that.
The alternative approach mentioned, i.e. to name all loggers explicitly
in the configuration file, is just too unintuitive.

@acooke
Copy link
Mannequin Author

acooke mannequin commented May 6, 2008

Got more important things than this to worry about, but yes, original
response not very helpful. In a perfect world this might be either
justified more clearly or addressed. OTOH, I get the impression hardly
anyone uses this package (docs were pretty opaque...)

Also, isn't this a "won't fix" rather than an "invalid"?

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant